Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: RegEx not right in my XmlSchema

From: billr@-----------.---------.---
To: NULL
Date: 10/1/2007 6:01:00 AM

"Bjoern Hoehrmann" wrote:

> It is neither good practise nor generally possible to distinguish be-
> tween these cases, certainly not in XML Schema. In short, what you seem
> to be trying to do cannot be made to work.

Thanks for your reply Bjoern, however, maybe a little background on the 
issue might help.

What we are trying to achieve is validation against an XmlSchema who has an 
element declaration for a particular node requiring content of the following 
format

  <![CDATA[ html markup goes here ]]>

If the Xml document does not contain such content for the named element then 
the document is deemed to be invalid.

... we are passing Xml to an Adobe Flash ActionScript object, it expects the 
xml in the following format

<someElement>
 <content><![CDATA[ <P>my <B>html</B> goes here</P> ]]></content>
</someElement>

and we want to validate the Xml PRIOR to it being transmitted to the 
ActionScript object.

We can do this using the XmlSchema 

<?xml version="1.0" encoding="utf-8" ?> 
<xs:schema targetNamespace="http://uri/test.xsd" 
elementFormDefault="qualified"
                  xmlns="http://uri/test.xsd" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="root">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="cd" type="CDATAContentElementType" />
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <xs:simpleType name="CDATAContentElementType">
    <xs:restriction base="xs:string">
      <xs:pattern value="\& 
lt;\!\[CDATA\[\s*(\d*|\w*|\W*|\s*|.*|>*)*\s*\]\]\& gt;" />
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

and the data ...

<root xmlns="http://uri/test.xsd">
  <cd>& lt;![CDATA[ <P>my <B>html</B> goes here</P> ]]& gt;</cd>
</root>

However, because the CDATA section declaration is escaped, the contents are 
not treated as a CDATA section per se, and the schema is then called upon to 
further validate the contents of the faux CDATA section (not required 
behaviour).

If the above xml were written without escaping the CDATA section declaration

<root xmlns="http://uri/test.xsd">
  <cd><![CDATA[ <P>my <B>html</B> goes here</P> ]]></cd>
</root>

the contents of that CDATA section are themselves not parsed during Schema 
validation (required behaviour)


********************
* SOLUTION DISCOVERED *
********************

The solution appears to be ... defining the content element as follows:

<xs:simpleType name="CDATAContentElementType">
    <xs:restriction base="xs:string">
      <xs:pattern value="\s*(\d*|\w*|\W*|\s*|.*|>*)*\s*" />
    </xs:restriction>
</xs:simpleType>

i.e. leave the <![CDATA[ ]]> part out of the RegEx pattern.

I have discovered that the VisualStudio IDE itself indicates that the CDATA 
declaration might be necessary. Therefore using the VS IDE we have the 
following successfully validated against the Schema

   <cd><![CDATA[ <P>my <B>html</B> goes here</P> ]]></cd>
   <cd>something here</cd>

which is fine in our particular case (because we are dealing with text that 
may or may not be html markup-ed), and the Schema disallows

        <cd><P>my <B>html</B> goes here</P></cd>

So, again (though not the response I was looking for) thanks for your reply

-- 
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent