2

如何向 xsd:any 元素添加属性?例如,给定以下内容:

<xsd:element name="requests">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:any namespace="http://xxx.yyy.com" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

如何将作为属性添加到 any 以便可以针对架构验证以下 xml 而不会出现错误:

<requests>
    <operation count="1">
<requests>
4

2 回答 2

0

xsd:any 不能包含任何属性声明,因为它本质上允许在命名空间“http://xxx.yyy.com”中定义的任何元素在序列中声明。如果您没有使用单独的 XSD 来验证该命名空间,那么您可以简单地使用以下代码代替 xsd:any:

<xsd:element name="operation">
   <xsd:complexType>
      <xsd:attribute name="count" type="nonNegativeInteger" use="required"/>
   </xsd:complexType>
</xsd:element>

否则,您将需要在 XSD 顶部为“http://xxx.yyy.com”声明名称空间前缀,并引用该架构中的元素而不是 xsd:any。因此,如果“http://xxx.yyy.com”的架构包含以下声明:

<xsd:complexType name="operationType">
   <xsd:attribute name="count" type="nonNegativeInteger" use="required"/>
</xsd:complexType>

然后你可以在你的 XSD 中引用这个类型:

<xsd:element name="requests">           
   <xsd:complexType>           
      <xsd:sequence>           
         <xsd:element type="optype:operationType"/>           
      </xsd:sequence>           
   </xsd:complexType>           
</xsd:element>  
于 2011-09-06T14:25:09.680 回答
0

如果您说要允许任何元素作为子元素,只要它具有 count 属性,那么您就不能在 XSD 1.0 中这样做。您可以在 XSD 1.1(目前在 Saxon 和 Xerces 中支持)中使用以下断言:

<xs:assert test="every $x in * satisfies (exists($x/@count) and $x/@count castable to xs:integer)"/> 
于 2011-09-06T16:18:30.417 回答