2010-04-13 96 views
3

我不認爲這是可能的,但我認爲我會把它扔到那裏。鑑於這種XML:使用XSD驗證節點數

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

是否有可能在一個XSD迫使@count屬性值來定義的元素(在這種情況下,人的要素)的正確的計數?上面的例子顯然是正確的,下面的例子不會驗證:

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

回答

4

我很確定XSD不能這樣做。但是,如果你要保證你的計數屬性爲以下元素的實數,其上運行的文件的XSLT樣式表可以確保將該值設置爲true:

<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 --> 
0

分析器執行XSD規範1.1應當提供斷言功能,可用於根據子節點的數量驗證XML內容