2012-03-23 2 views
2

Мой XML документ выглядит this-Как применить xslt для каждого к attibutes?

<doc> 

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

</doc> 

Я хотел бы преобразовать его, как «идентификатор» значение атрибута увеличивается на единицу, например, последовательно:

<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» дизайн и my 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 выглядит this-

<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, я получаю следующее error-

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-not-created-after-the-children-of-the-contains – bouncingHippo

ответ

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

+0

@ user837208 - Добро пожаловать! –

+0

Вы можете помочь с подобной проблемой http://stackoverflow.com/questions/12289248/xslt-attribute-node-id-cannot-be-created-after-the-children-of-the-containing – bouncingHippo

Смежные вопросы