简体   繁体   中英

XSD - Ignoring any Content of XML Element

I'm yet searching for some time for a solution for a problem with some database exports looking as follows:

<DATA>

<ROW>
    <ID>36342</ID>
    <TIMESTAMP1>2017-11-24 14:54:51</TIMESTAMP1>
    <TIMESTAMP2>2017-11-24 14:54:51</TIMESTAMP2>
    <NOTES>please try <variable_name> inside <function></NOTES>
</ROW>

<ROW>
    <ID>36341</ID>
    <TIMESTAMP1>2017-11-24 12:51:50</TIMESTAMP1>
    <TIMESTAMP2>2017-11-24 12:51:50</TIMESTAMP2>
    <NOTES>Otherwise <option> could also be</NOTES>
</ROW>
</DATA>

The real data in use is in fact a Support Database, means there are multiple place holders in hundreds of thousands of Entrys (NOTES2), so of course I cannot change any content. Also the Notes can contain any number of place holders, including none.

Now the problem is about, how to be able to understand any Content of "NOTES" as plain text, ignoring all XML lookalikes like <function> , <variable_name> and <option> . (and of course many others)

What I've got yet:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
        xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
<xsd:element name="DATA" sql:is-constant="1" >  
 <xsd:complexType>  
   <xsd:sequence>  
     <xsd:element name="ROW" sql:relation="document" maxOccurs="unbounded">  
       <xsd:complexType>  
         <xsd:sequence>  
           <xsd:element name="ID"  type="xsd:string" />  
           <xsd:element name="TIMESTAMP1" type="xsd:string" />  
           <xsd:element name="TIMESTAMP2" type="xsd:string" />
                <xsd:element name="NOTES" type="string" >
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:any processContents="skip" minOccurs="0"/> 
                        </xsd:sequence>    
                    </xsd:complexType>
                </xsd:element>
         </xsd:sequence>  
       </xsd:complexType>  
     </xsd:element>  
   </xsd:sequence>  
  </xsd:complexType>  
 </xsd:element>  
 </xsd:schema>

I have to add that I am in fact not an expert in XML/XSD. I'm only able to understand what I read. What I do read is, any Tag found inside "NOTES" should be skipped on interpretion. But unfortunately the Parser says:

 'type' cannot be present with either 'simpleType' or 'complexType'

So of course the error message tells me to delete the entity "type" of the Element "NOTES", but if I do so the error is:

Text is not allowed in the context of element 'NOTES' according to DTD/Schema.

So how would I now get the full content of such an element?

Best regards Marius

XSD cannot be written to allow XML not to be well-formed .

You cannot define an XSD to allow

<NOTES>please try <variable_name> inside <function></NOTES>

because such data is not well-formed XML. A < character cannot be used unescaped in XML without serving as the start of markup. Nothing you can do in XSD can change this.

Options

  1. Escape < :

     <NOTES>please try &lt;variable_name> inside &lt;function></NOTES> 
  2. Use CDATA:

     <NOTES><![CDATA[please try <variable_name> inside <function>]]></NOTES> 
  3. Or close the "elements":

     <NOTES>please try <variable_name/> inside <function/></NOTES> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM