按位置迭代和抓取节点



我想复制重复结构中的xml节点到另一个重复结构的位置…意思是第一个节点到第一个节点,依此类推。

试试这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="error" match="XmlerrorItmOut/item" use="count(preceding-sibling::item) + 1"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Xmlitem/item">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:copy-of select="key('error', position())/YyextError"/>
      <xsl:copy-of select="key('error', position())/YyerrorDesc"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

首先创建key。我们使用计数前兄弟节点而不是位置。虽然如果你有数千个项目,它可能会很慢,但它是有效的。

单位变换"按原样"复制每个节点。

第二个模板匹配任何Xmlitem/item元素,复制它的内容并按键添加错误元素。

最新更新