Using XSD to check node count

I do not think this is possible, but I thought I would throw it there. Given this XML:

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

Is it possible in XSD to force the value of the @count attribute to be the correct counter for certain elements (in this case, the person element)? The above example will obviously be correct, and the example below will not check:

 <people count="5">
      <person>Bill</person>
      <person>Joe</person>
      <person>Susan</person>
 </people>
+3
source share
2 answers

I'm sure XSD cannot do this. However, if you want to ensure that your count attribute is the actual number of elements below, running the XSLT stylesheet in the document can ensure that this 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 -->
+4
source

, XSD Specification 1.1, assert, XML

0

Source: https://habr.com/ru/post/1740994/


All Articles