簡體   English   中英

XSD:將element的屬性值約束為父屬性的子字符串

[英]XSD: constrain attribute value of element to a substring of parent attribute

考慮一下這段XML

<Parent id="MyParent1">
   <Child id="MyParent1.MyChild1"/>
</Parent>

<Parent id="MyParent2">
   <Child id="MyParent2.MyChild1"/>
   <Child id="MySillyIncorrectPrefix.MyChild2"/>
</Parent>

如何驗證(可能使用XSD)其id包含父元素id作為前綴的子元素,以便:

<Child id="MyParent2.MyChild1"/> <!-- is valid -->
<Child id="MySillyIncorrectPrefix.MyChild2"/> <!-- is not valid -->

我沒有綁定XSD版本1.0,所以我可以嘗試使用XSD 1.1(和斷言等功能),但我想知道:

  • 如何用xs:assertion或更合適的XSD 1.1特性表達上述約束。
  • 如果可以利用該xsd在Java中構建驗證器? “Xerces-J”是一個可行的解決方案嗎?

在我對XSD 1.1的有限知識中,我想出了這個嘗試:

<xs:element name="Child"> 
  <xs:complexType>
     <xs:attribute name="type" type="xs:String" />
     <xs:assert test="starts-with(@type,../@type)"/>
</xs:complexType> 
</xs:element>

這個對嗎? 有沒有可以測試它的工具? 更一般:有沒有工具來幫助構建和測試這種XSD 1.1特色模式? (afaik Eclipse僅支持XSD 1.0)

您所描述的內容在XSD 1.0中是不可行的。

在XSD 1.1中,您可以對父類型使用斷言,要求每個子節點的id屬性以父節點id屬性的值開頭。

此架構應驗證該示例(使用xsd 1.1斷言)

<?xml version="1.1" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/stackoverflow" xmlns:tns="http://www.example.org/stackoverflow" elementFormDefault="qualified">

    <complexType name="ParentType">
        <sequence>
            <element name="Child" type="tns:ChildType"
                maxOccurs="unbounded" minOccurs="0">
            </element>
        </sequence>
        <attribute name="id" type="string"></attribute> 
    </complexType>

    <element name="Parent" type="tns:ParentType"></element>

    <complexType name="ChildType">
        <attribute name="id" type="string"></attribute>
        <assert test="starts-with(@id,../@id)"/>
    </complexType>

</schema>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM