tags:

views:

77

answers:

2

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>
+4  A: 

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 -->
Jweede
A: 

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

Wamiq Bashir