我有下面的XML文檔XSLT節點計數器
<root>
<TotalPeople>BLABLA</TotalPeople>
<MoreTagsWithData/>
<Person>
<id>bla-bla</id>
<Name>John Smith</Name>
<MoreTagsWithData/>
</Person>
<Person>
<id>bla-bla</id>
<Name>John Doe</Name>
<MoreTagsWithData/>
</Person>
</root>
我需要得到
<root>
<TotalPeople>2</TotalPeople> <!-- Needs to calculate how many "Person" tags -->
<MoreTagsWithData/>
<Person>
<id>1</id> <!-- incrementing per each Person -->
<Name>John Smith</Name>
<MoreTagsWithData/>
</Person>
<Person>
<id>2</id>
<Name>John Doe</Name>
<MoreTagsWithData/>
</Person>
</root>
我試圖XSLT模板文件:
<!-- copy all file (I need to save the whole file since in reality it contains much more data -->
<xsl:template match="*|@*|text()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- indexing ids -->
<xsl:template match="id">
<xsl:copy>
<xsl:number level="any"/>
</xsl:copy>
</xsl:template>
<!-- Piece I am not sure , it does not work -->
<xsl:template match="TotalPeople">
<xsl:copy>
<xsl:number level="any" count="/root/Person/id"/>
</xsl:copy>
</xsl:template>
我需要計算文檔中有多少個標籤,並使用此值修改文檔中的特殊標籤。我無法創建新文檔,因爲真正的文檔包含很多信息,我需要維護它。
<xsl:template match="TotalPeople">
<xsl:copy>
<xsl:value-of select="count(/root/Person/id)"/>
<!-- or just select="count(/root/Person)" -->
</xsl:copy>
</xsl:template>
XSLT樣式表被設計成一個輸入XML文檔轉換到一個單獨的輸出文檔,而不是編輯就地文件:
有趣(我只用撒克遜-HE 9.4。測試這一點)......我還沒有意識到,你能做到這一點。如果覆蓋輸入文檔,是否保證在讀完原稿之前不會開始覆蓋? – LarsH
@LarsH - 我不確定。由於不是流式傳輸,我認爲整個輸入必須在處理/覆蓋開始之前被讀取和存儲在內存中。我只做了兩次,兩次都是堆棧溢出的答案。測試時我沒有遇到任何問題,但數據集很小。 –