1

I want to validate the following XML through schema where it should not be validated if there is no text inside the l10n element. Also the content element is mandatory for the XML. Please someone suggest me the relevant answer.

The XML to validate would be:

<body xmlns="http://iddn.icis.com/ns/test">
   <content>
      <l10n xml:lang="en"></l10n>
   </content>
</body>

The schema I am currently using is as follows but it is still allowing the above XML to be validated.

<xs:schema elementFormDefault="qualified" targetNamespace="http://iddn.icis.com/ns/test" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:conf="http://iddn.icis.com/ns/config" 
xmlns="http://www.w3.org/1999/xhtml">
<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd">
</xs:import>
<xs:simpleType name="nameType">  
    <xs:restriction base="xs:string">  
        <xs:minLength value="1"/>   
    </xs:restriction>  
</xs:simpleType>

<xs:element name="content">
      <xs:complexType mixed="true">
         <xs:sequence>
            <xs:any maxOccurs="unbounded" minOccurs="0" processContents="skip"/>
         </xs:sequence>
      </xs:complexType>
   </xs:element>

<xs:complexType name="i18n-value">
      <xs:sequence>
         <xs:element name="l10n" maxOccurs="unbounded">
            <xs:complexType>
               <xs:simpleContent>
                  <xs:extension base="nameType">
                     <xs:attribute ref="xml:lang" use="required"/>
                  </xs:extension>
               </xs:simpleContent>
            </xs:complexType>
         </xs:element>
      </xs:sequence>
   </xs:complexType>

<xs:element name="body">
  <xs:complexType>
    <xs:sequence><xs:element ref="content"></xs:element></xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
rgCoder
  • 27
  • 1
  • 7

1 Answers1

1

There are numerous problems here at multiple levels. Let's tackle them collectively in two phases: First, we'll repair the XSD problems that are preventing any sort of validation from taking place.

Repairing initial XSD problems

This XML

<?xml version="1.0" encoding="utf-16"?>
<body xmlns="http://iddn.icis.com/ns/test"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://iddn.icis.com/ns/test try.xsd">
  <content>
    <l10n xml:lang="en"></l10n>
  </content>
</body>

is valid against this XSD:

<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://iddn.icis.com/ns/test"
           xmlns:tst="http://iddn.icis.com/ns/test">
  <xs:import namespace="http://www.w3.org/XML/1998/namespace"
             schemaLocation="http://www.w3.org/2001/xml.xsd"/>

  <xs:simpleType name="nameType">  
    <xs:restriction base="xs:string">  
      <xs:minLength value="1"/>
    </xs:restriction>  
  </xs:simpleType>

  <xs:element name="content">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:any maxOccurs="unbounded" minOccurs="0" processContents="skip"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="i18n-value">
    <xs:sequence>
      <xs:element name="l10n" maxOccurs="unbounded">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="tst:nameType">
              <xs:attribute ref="xml:lang" use="required"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="body">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="tst:content"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Now, your constraint that l10n not be empty still remains to be addressed. Here's how to do this:

  • Change xsd:any/@processContents from skip to lax so that you can affect validation beneath xsd:any via element declarations. Read more about the differences here: processContents strict vs lax vs skip for xsd:any:

  • Actually define an xs:element for i18n-value; having a type definition alone isn't sufficient.

  • Add i18n-value to your XML.

Altogether...

Final XSD and XML

XML

<?xml version="1.0" encoding="utf-16"?>
<body xmlns="http://iddn.icis.com/ns/test"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://iddn.icis.com/ns/test try.xsd">
  <content>
    <i18n-value>
      <l10n xml:lang="en"></l10n>
    </i18n-value>
  </content>
</body>

XSD

<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://iddn.icis.com/ns/test"
           xmlns:tst="http://iddn.icis.com/ns/test">
  <xs:import namespace="http://www.w3.org/XML/1998/namespace"
             schemaLocation="http://www.w3.org/2001/xml.xsd"/>

  <xs:simpleType name="nameType">
    <xs:restriction base="xs:string">  
      <xs:minLength value="1"/>
    </xs:restriction>  
  </xs:simpleType>

  <xs:element name="content">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:any maxOccurs="unbounded" minOccurs="0" processContents="lax"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="i18n-value">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="l10n" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="tst:nameType">
                <xs:attribute ref="xml:lang" use="required"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="body">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="tst:content"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks for your response Kenneth. In my case, body element should be of type i18n-value. Sorry, I missed it in initial question. – rgCoder Mar 21 '16 at 07:08
  • Given that the answer I provided fixed numerous mistakes in your original XSD and worked perfectly for your original XML, I ask that you upvote and/or [**accept**](http://meta.stackoverflow.com/q/5234/234215) this answer and ask a new one if you need further help. Your changed XML really constitutes a completely new question. Thanks. – kjhughes Mar 21 '16 at 12:24
  • Can you please suggest me Kenneth the answer for the question I just out up in my previous comment? Thanks in advance. – rgCoder Mar 22 '16 at 09:06
  • I'd be happy to look at a new question posted as a new question, not in a comment. As long as you tag it with [xsd] and/or related tags, I'll see it. Thanks. – kjhughes Mar 22 '16 at 12:54