Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XSD to validate node count

Tags:

count

nodes

xsd

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>
      <person>Susan</person>
 </people>
like image 674
heath Avatar asked Mar 11 '26 02:03

heath


1 Answers

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 -->
like image 126
Jon W Avatar answered Mar 12 '26 22:03

Jon W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!