Validate descendants of unknown XML elements via XSD?

↘锁芯ラ 提交于 2019-12-24 18:02:07

问题


My XML file looks like :

<root>
    <template>
        <unknownTag>
            <anotherUnknownTag/>
            <anotherKnownTag/>
            <field name='price'/>
        </unknownTag>
    </template>
    <template>
        <field name='salary'/>
    </template>
    <anothorKnownTag/>
</root>

I want to apply regex restriction to the attribute name of the tag <field/> whether it's a child or a grandchild or a grand grandchild and so on. I have tried the following code, but the regex only applied to the element field when it's a direct child of the template tag.

  <xs:element name="template">
    <xs:complexType>
        <xs:complexContent>
        <xs:sequence>
            <xs:element name="field">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="name">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="[a-z][a-z_]*"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:attribute>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
            <xs:any processContents="lax"/>
        </xs:sequence>
    </xs:complexType>
  </xs:element>

回答1:


You can actually express the requested constraints in XSD 1.0:

XSD 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="field">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="name">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:pattern value="[a-z][a-z_]*"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

Note: You can even simplify root to be just

  <xs:element name="root"/>

but the longer form is less cryptic.

Valid XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <template>
        <unknownTag>
            <anotherUnknownTag/>
            <anotherKnownTag/>
            <field name="price"/>
        </unknownTag>
    </template>
    <template>
        <field name="salary"/>
    </template>
    <anothorKnownTag/>
</root>

Invalid XML

The following XML has two validity errors due to field/@name values not matching the regex:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <template>
        <unknownTag>
            <anotherUnknownTag/>
            <anotherKnownTag/>
            <field name="price999"/>
        </unknownTag>
    </template>
    <template>
        <field name="big salary"/>
    </template>
    <anothorKnownTag/>
</root>


来源:https://stackoverflow.com/questions/38340411/validate-descendants-of-unknown-xml-elements-via-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!