Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Detect Invalid sequence of tag in XML document using schema.

Detect Invalid sequence of tag in XML document using schema.

Scheduled Pinned Locked Moved XML / XSL
xmldatabasetutorialquestion
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T tprakash

    I have xml tags as below. file1.xml <Roles> <Role> <Permissions>Admin<Permissions/> <Permissions>Reader<Permissions/> </Role> </Roles> Here is my XSD to validate above xml myXSD.xsd <element name="Roles"> <complexType> <sequence> <element ref="Role"> </sequence> </complexType> </element> <element name ="Role"> <complexType> <sequence> <element ref ="Permissions" minOccurs="1" maxOccurs="unbounded" /> </sequence> </complexType> </element> <element name ="Permissions"> <complexType> <sequence> <element name ="Permission" type="string" /> </sequence/> </complexType/> </element/> This XSD validation works fine in normal case. If I change the xml file as below. file2.xml <Roles> <Role> <Permissions>Admin<Permissions/> <Permissions>Reader<Permissions/> </Role> <Permissions>Reader<Permissions/> </Roles> ie. I inserted the <Permissions> immediately after the <Roles> parent node. This is invalid in my case. I want to enforce that <Permissions> node always comes inside the <Role> node but not <Roles> node. I want to detect this such case using XSD validation. Could you please suggest me how to write the correct XSD schema to invalidate file2.xml? Thanks in advance Prakash

    S Offline
    S Offline
    Stuart Dootson
    wrote on last edited by
    #2

    Neither of your files should validate. I suspect your schema isn't parsing correctly, you're not checking errors and thinking that validation's happening. Your problems?

    1. You can't use the ref attribute of the element element without using some level of XML namespaces.
    2. You've omitted the Permission elements from your sample files
    3. Your closing Permissions tags are incorrect - I'll presume that's a typo

    Here's an xsd/xml pair that does cause the validation error you want (at least with libxml2): XSD file

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema
    targetNamespace="http://test-namespace.com/"
    xmlns="http://test-namespace.com/"
    xmlns:srd="http://test-namespace.com/"
    elementFormDefault="qualified" attributeFormDefault="unqualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="Roles">
    xs:complexType
    xs:sequence
    <xs:element ref ="Role"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:element name ="Role">
    xs:complexType
    xs:sequence
    <xs:element ref="Permissions"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:element name ="Permissions">
    xs:complexType
    xs:sequence
    <xs:element name ="Permission" type="xs:string" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    </xs:schema>

    XML file

    <?xml version="1.0" encoding="utf-8"?>
    <Roles xmlns="http://test-namespace.com/">
    <Role>
    <Permissions>
    <Permission>Reader</Permission>
    </Permissions>
    </Role>
    <Permissions>
    <Permission>Reader</Permission>
    </Permissions>
    </Roles>

    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

    T 2 Replies Last reply
    0
    • S Stuart Dootson

      Neither of your files should validate. I suspect your schema isn't parsing correctly, you're not checking errors and thinking that validation's happening. Your problems?

      1. You can't use the ref attribute of the element element without using some level of XML namespaces.
      2. You've omitted the Permission elements from your sample files
      3. Your closing Permissions tags are incorrect - I'll presume that's a typo

      Here's an xsd/xml pair that does cause the validation error you want (at least with libxml2): XSD file

      <?xml version="1.0" encoding="utf-8"?>
      <xs:schema
      targetNamespace="http://test-namespace.com/"
      xmlns="http://test-namespace.com/"
      xmlns:srd="http://test-namespace.com/"
      elementFormDefault="qualified" attributeFormDefault="unqualified"
      xmlns:xs="http://www.w3.org/2001/XMLSchema">

      <xs:element name="Roles">
      xs:complexType
      xs:sequence
      <xs:element ref ="Role"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      <xs:element name ="Role">
      xs:complexType
      xs:sequence
      <xs:element ref="Permissions"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      <xs:element name ="Permissions">
      xs:complexType
      xs:sequence
      <xs:element name ="Permission" type="xs:string" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      </xs:schema>

      XML file

      <?xml version="1.0" encoding="utf-8"?>
      <Roles xmlns="http://test-namespace.com/">
      <Role>
      <Permissions>
      <Permission>Reader</Permission>
      </Permissions>
      </Role>
      <Permissions>
      <Permission>Reader</Permission>
      </Permissions>
      </Roles>

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      T Offline
      T Offline
      tprakash
      wrote on last edited by
      #3

      Thank you very much. It worked perfectly :) .

      1 Reply Last reply
      0
      • S Stuart Dootson

        Neither of your files should validate. I suspect your schema isn't parsing correctly, you're not checking errors and thinking that validation's happening. Your problems?

        1. You can't use the ref attribute of the element element without using some level of XML namespaces.
        2. You've omitted the Permission elements from your sample files
        3. Your closing Permissions tags are incorrect - I'll presume that's a typo

        Here's an xsd/xml pair that does cause the validation error you want (at least with libxml2): XSD file

        <?xml version="1.0" encoding="utf-8"?>
        <xs:schema
        targetNamespace="http://test-namespace.com/"
        xmlns="http://test-namespace.com/"
        xmlns:srd="http://test-namespace.com/"
        elementFormDefault="qualified" attributeFormDefault="unqualified"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">

        <xs:element name="Roles">
        xs:complexType
        xs:sequence
        <xs:element ref ="Role"/>
        </xs:sequence>
        </xs:complexType>
        </xs:element>

        <xs:element name ="Role">
        xs:complexType
        xs:sequence
        <xs:element ref="Permissions"/>
        </xs:sequence>
        </xs:complexType>
        </xs:element>

        <xs:element name ="Permissions">
        xs:complexType
        xs:sequence
        <xs:element name ="Permission" type="xs:string" />
        </xs:sequence>
        </xs:complexType>
        </xs:element>

        </xs:schema>

        XML file

        <?xml version="1.0" encoding="utf-8"?>
        <Roles xmlns="http://test-namespace.com/">
        <Role>
        <Permissions>
        <Permission>Reader</Permission>
        </Permissions>
        </Role>
        <Permissions>
        <Permission>Reader</Permission>
        </Permissions>
        </Roles>

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        T Offline
        T Offline
        tprakash
        wrote on last edited by
        #4

        Hi, As you said we cannot use 'ref' attribute in xsd without using some level of xml namespace. Can we write a schema file (xsd) to valid the xml without having xmlns? Is there any other alternative to valid the xml without having xmlns? I mean, can we write xsd to valid the following xml? <?xml version="1.0" encoding="utf-8"?> <Roles> <Role> <Permissions> <Permission>Reader</Permission> </Permissions> </Role> <Permissions> <Permission>Reader</Permission> </Permissions> </Roles> Thanks. Prakash

        S 1 Reply Last reply
        0
        • T tprakash

          Hi, As you said we cannot use 'ref' attribute in xsd without using some level of xml namespace. Can we write a schema file (xsd) to valid the xml without having xmlns? Is there any other alternative to valid the xml without having xmlns? I mean, can we write xsd to valid the following xml? <?xml version="1.0" encoding="utf-8"?> <Roles> <Role> <Permissions> <Permission>Reader</Permission> </Permissions> </Role> <Permissions> <Permission>Reader</Permission> </Permissions> </Roles> Thanks. Prakash

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #5

          a) what's the hassle with using XML namespaces? Really isn't that much trouble. b) You can use something like this without namespaces. The references have effectively been manually resolved:

          <?xml version="1.0" encoding="utf-8"?>
          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

          <xs:element name="Roles">
          xs:complexType
          xs:sequence
          <xs:element name ="Role">
          xs:complexType
          xs:sequence
          <xs:element name ="Permissions">
          xs:complexType
          xs:sequence
          <xs:element name ="Permission" type="xs:string" />
          </xs:sequence>
          </xs:complexType>
          </xs:element>
          </xs:sequence>
          </xs:complexType>
          </xs:element>
          </xs:sequence>
          </xs:complexType>
          </xs:element>

          </xs:schema>

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          T 2 Replies Last reply
          0
          • S Stuart Dootson

            a) what's the hassle with using XML namespaces? Really isn't that much trouble. b) You can use something like this without namespaces. The references have effectively been manually resolved:

            <?xml version="1.0" encoding="utf-8"?>
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

            <xs:element name="Roles">
            xs:complexType
            xs:sequence
            <xs:element name ="Role">
            xs:complexType
            xs:sequence
            <xs:element name ="Permissions">
            xs:complexType
            xs:sequence
            <xs:element name ="Permission" type="xs:string" />
            </xs:sequence>
            </xs:complexType>
            </xs:element>
            </xs:sequence>
            </xs:complexType>
            </xs:element>
            </xs:sequence>
            </xs:complexType>
            </xs:element>

            </xs:schema>

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            T Offline
            T Offline
            tprakash
            wrote on last edited by
            #6

            a) The XML file I am validating is from my clients. They cannot bear any change modification on that xml because it is being used by many others clients. They already working on the format of that xml. If I add an xml namespace in the XML file, all of my clients have to change their logic to read the modified XML file. What I can do is to write the xsd file to validate that XML file. It wouldn't have been problem if I could use namespace. I think your answer (b) will work for me. Thank your great help. Prakash

            S 1 Reply Last reply
            0
            • S Stuart Dootson

              a) what's the hassle with using XML namespaces? Really isn't that much trouble. b) You can use something like this without namespaces. The references have effectively been manually resolved:

              <?xml version="1.0" encoding="utf-8"?>
              <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

              <xs:element name="Roles">
              xs:complexType
              xs:sequence
              <xs:element name ="Role">
              xs:complexType
              xs:sequence
              <xs:element name ="Permissions">
              xs:complexType
              xs:sequence
              <xs:element name ="Permission" type="xs:string" />
              </xs:sequence>
              </xs:complexType>
              </xs:element>
              </xs:sequence>
              </xs:complexType>
              </xs:element>
              </xs:sequence>
              </xs:complexType>
              </xs:element>

              </xs:schema>

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              T Offline
              T Offline
              tprakash
              wrote on last edited by
              #7

              I think the solution (b) works perfectly in cases where we have normal xml node. What about if the a child xml node contains parent xml node recursively? Taken the above case, what if Permissions node in turn contains the Roles ? Since we cannot use ref attribute, how to refer the Roles node in the above xsd? Thanks Prakash

              S 1 Reply Last reply
              0
              • T tprakash

                a) The XML file I am validating is from my clients. They cannot bear any change modification on that xml because it is being used by many others clients. They already working on the format of that xml. If I add an xml namespace in the XML file, all of my clients have to change their logic to read the modified XML file. What I can do is to write the xsd file to validate that XML file. It wouldn't have been problem if I could use namespace. I think your answer (b) will work for me. Thank your great help. Prakash

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #8

                tprakash wrote:

                The XML file I am validating is from my clients. They cannot bear any change modification on that xml because it is being used by many others clients

                That I can understand. I presume your clients don't have a schema for their XML? In which case, what guarantees do you have that their XML will always match any schema you write? And how did they specify the XML format to you?

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                T 1 Reply Last reply
                0
                • T tprakash

                  I think the solution (b) works perfectly in cases where we have normal xml node. What about if the a child xml node contains parent xml node recursively? Taken the above case, what if Permissions node in turn contains the Roles ? Since we cannot use ref attribute, how to refer the Roles node in the above xsd? Thanks Prakash

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #9

                  As I said in this message[^], I think I was wrong about not being able to use ref attributes. I'm not sure what was going on in my head that day :-) This schema validates your XML with libxml2 2.7.3 (which is a pretty recent release). I've put in the maxOccurs attribute of the sequence that contains the Permission element to enable multiple Permission elements in a Permissions element.

                  <?xml version="1.0" encoding="utf-8"?>
                  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

                  <xs:element name="Roles">
                  xs:complexType
                  xs:sequence
                  <xs:element ref ="Role"/>
                  </xs:sequence>
                  </xs:complexType>
                  </xs:element>

                  <xs:element name ="Role">
                  xs:complexType
                  xs:sequence
                  <xs:element ref="Permissions"/>
                  </xs:sequence>
                  </xs:complexType>
                  </xs:element>

                  <xs:element name ="Permissions">
                  xs:complexType
                  <xs:sequence maxOccurs="unbounded">
                  <xs:element name ="Permission" type="xs:string" />
                  </xs:sequence>
                  </xs:complexType>
                  </xs:element>

                  </xs:schema>

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  T 1 Reply Last reply
                  0
                  • S Stuart Dootson

                    tprakash wrote:

                    The XML file I am validating is from my clients. They cannot bear any change modification on that xml because it is being used by many others clients

                    That I can understand. I presume your clients don't have a schema for their XML? In which case, what guarantees do you have that their XML will always match any schema you write? And how did they specify the XML format to you?

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    T Offline
                    T Offline
                    tprakash
                    wrote on last edited by
                    #10

                    Actually, we have two applications, one generates the xml, while other application consume the xml. The format of xml is defined by my management team. We have different clients using that applications(single only or both). And it is our management team decision not to change the format of the xml file. We just donot want to mesh up with that now. Otherwise, it is ok.

                    1 Reply Last reply
                    0
                    • S Stuart Dootson

                      As I said in this message[^], I think I was wrong about not being able to use ref attributes. I'm not sure what was going on in my head that day :-) This schema validates your XML with libxml2 2.7.3 (which is a pretty recent release). I've put in the maxOccurs attribute of the sequence that contains the Permission element to enable multiple Permission elements in a Permissions element.

                      <?xml version="1.0" encoding="utf-8"?>
                      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

                      <xs:element name="Roles">
                      xs:complexType
                      xs:sequence
                      <xs:element ref ="Role"/>
                      </xs:sequence>
                      </xs:complexType>
                      </xs:element>

                      <xs:element name ="Role">
                      xs:complexType
                      xs:sequence
                      <xs:element ref="Permissions"/>
                      </xs:sequence>
                      </xs:complexType>
                      </xs:element>

                      <xs:element name ="Permissions">
                      xs:complexType
                      <xs:sequence maxOccurs="unbounded">
                      <xs:element name ="Permission" type="xs:string" />
                      </xs:sequence>
                      </xs:complexType>
                      </xs:element>

                      </xs:schema>

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      T Offline
                      T Offline
                      tprakash
                      wrote on last edited by
                      #11

                      Finally, I could create the desired xsd to valid the xml. I truly appreciate your great help. Thanks a lot. :) Prakash

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups