开发者

Using XSD to validate node count

开发者 https://www.devze.com 2022-12-26 13:33 出处:网络
I don\'t think this is possible but I thought I\'d throw it out there.Given this XML: <people count=\"3\">

I don't think this is possible but I thought I'd throw it out there. Given this XML:

 <people count="3">
      <person>Bill</person>
      <person>Joe</person>
      <person>Susan</person>
 </people>

Is it possible in an XSD to force the @count attribute value to be the correct count of defined elements (in this case, the person element)? The above example would obviously be correct and the below example would not validate:

 <people count="5">
      <person>Bill</person>
      <person>Joe</person>
      <p开发者_开发技巧erson>Susan</person>
 </people>


I'm pretty sure XSD can't do that. However if you want to guarantee that your count attribute is the real count of elements below, running a XSLT stylesheet on the document can ensure that is true by setting the value:

<xsl:template match="people">
   <xsl:attribute name="count">
      <xsl:value-of select="count(person)"/>
   </xsl:attibute>
   <xsl:apply-templates/>
</xsl:template>

<!-- insert your identity template here -->


Parsers implementing XSD Specification 1.1 should provide an assert function that can be used to validate the XML content against number of child nodes

0

精彩评论

暂无评论...
验证码 换一张
取 消