Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

xml - XSD and where to apply pattern to child tag that has an attribute

The XML validates against the XSD. Now, I'm having trouble where/how to add a pattern tag like the following to the B tag:

<xs:pattern value="(?i)[a-z0-9]+@[a-z0-9]+.[a-z0-9]{3}"/>

I've tried chaining together an extension of the attribute with a restriction using the tag but it's just not working out. I've looked high and low for examples and solutions of this scenario but haven't found anything. I'm clearly missing something here.

Where can I put in a restriction to validate the content of the <B> tag?

My example XML is this:

<ImportA>
    <A>
        <Id>A1</Id>
        <Bs>
            <B priority="true">[email protected]</B>
            <B>[email protected]</B>
        </Bs>
    </A>
</ImportA>

I have the following XSD:

<xs:element name="ImportA">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="A" type="AType" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="AType">
    <xs:sequence>
        <xs:element name="Id" type="xs:string" minOccurs="1" maxOccurs="1"/>
        <xs:element name="Bs" minOccurs="0" maxOccurs="1">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="B" type="BType" minOccurs="0" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="BType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="priority" type="PriorityOrEmptyType"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

<xs:simpleType name="Priority">
    <xs:restriction base="xs:string">
        <xs:pattern value="(?i)true"/>
        <xs:pattern value="(?i)false"/>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="PriorityOrEmptyType">
    <xs:union memberTypes="Priority EmptyStringType"/>
</xs:simpleType>

<xs:simpleType name="EmptyStringType">
    <xs:restriction base="xs:string">
        <xs:enumeration value=""/>
    </xs:restriction>
</xs:simpleType>

Any help would be appreciated. Thanks!

question from:https://stackoverflow.com/questions/66051618/xsd-and-where-to-apply-pattern-to-child-tag-that-has-an-attribute

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've tried chaining together an extension of the attribute with a restriction using the tag

You cannot extend/restrict either attributes or tags. You can only extend/restrict type definitions.

but it's just not working out

Ideally, you should have told us what you tried - that way, we would know what you tried, and where your understanding was slightly off.

Where can I put in a restriction to validate the content of the tag?

Actually, this is easier than you think. You put it in the same place as for a normal tag - on the tag's simple type definition. In this case, the type definition that describes the text value of the tag is the simple type that you have extended. So...

Instead of

<xs:complexType name="BType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
        ...

Try:

<xs:simpleType name="EmailType">
    <xs:restriction base="xs:string">
        <xs:pattern value="(?i)[a-z0-9]+@[a-z0-9]+.[a-z0-9]{3}"/>
    </xs:restriction>
</xs:simpleType>

<xs:complexType name="BType">
    <xs:simpleContent>
        <xs:extension base="EmailType">
       ...

(Not tested, but something like that should do the job)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...