【问题标题】:How to apply xslt for-each to attributes?如何将 xslt for-each 应用于属性?
【发布时间】:2012-04-07 18:30:23
【问题描述】:

我的 XML 文档如下所示-

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

我想对其进行转换,例如“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

【问题讨论】:

标签: xslt saxon


【解决方案1】:

尝试将match="//system_data/*" 的模板更改为:

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

【讨论】:

猜你喜欢
  • 2015-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多