2012-03-23 50 views
2

我的XML文檔看起來像這 -如何將xslt for-each應用於attibutes?

​​

我願意把它的變換,例如「id」屬性值遞增順序如:

<doc> 

    <system_data> 
    <registry_item id="10"> 
     <hive>HKEY_LOCAL_MACHINE</hive> 
    </registry_item> 
    <file_item id="11"> 
     <filepath>C:\Windows\System32\msasn1.dll</filepath>  
    </file_item> 
    </system_data> 

</doc> 

我現在用的是「identity」設計我的xsl看起來像這樣 -

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
           xmlns:xslt="http://www.w3.org/1999/XSL/Transform"> 
<xsl:import href="Identity.xsl"/> 
<xslt:param name="newID" /> 
    <xsl:template match="//system_data/*"> 
     <xsl:for-each select="@id"> 
     <xsl:attribute name="id"> 
      <xsl:value-of select="number($newID)+1"/>  
     </xsl:attribute> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

我將newID值傳遞爲「10」。如何選擇每個屬性並增加其值?

Indentity.xsl看起來像這 -

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <!-- Whenever you match any node or any attribute --> 
    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

當我申請XSL,我收到以下錯誤 -

An attribute node (id) cannot be created after the children of the containing element 
+0

你好,你能夠幫助一個類似的問題http://stackoverflow.com/questions/12289248/xslt-attribut e-node-id-can-be-created-after-the-of-the-of-the-of-the-the-containing-the-of-the-of-the-containing-containing- – bouncingHippo 2012-09-06 15:18:16

回答

3

試着改變你的模板match="//system_data/*"這樣:

<xsl:template match="@id"> 
    <xsl:attribute name="id"> 
     <xsl:value-of select="number($newID)+count(preceding::*/@id)"/> 
    </xsl:attribute> 
    </xsl:template> 
+0

謝謝!這工作得很好。 – user837208 2012-03-23 07:10:40

+0

@ user837208 - 非常歡迎您! – 2012-03-23 14:03:08

+0

你能幫助類似的問題http://stackoverflow.com/questions/12289248/xslt-attribute-node-id-cannot-be-created-after-the-children-of-the-containing – bouncingHippo 2012-09-06 15:17:52