【问题标题】:Xsl: Copy For Each while ignoring all nodes that don't contain a certain type of nodeXsl:Copy For Each,同时忽略不包含特定类型节点的所有节点
【发布时间】:2017-03-30 20:06:12
【问题描述】:

<xsl:output indent="yes"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="root">
    <xsl:copy>
        <xsl:for-each-group select="RemittanceInformation" group-by="IndividualRemittance/ExchangeAssignedPolicyID">
          <xsl:copy>
              <xsl:variable name="pos" select="position()"/>
              <xsl:apply-templates select="EntityAssignedNumber, IndividualRemmittance, current-group()/RemittanceDetail">
                  <xsl:with-param name="pos" select="$pos"/>
              </xsl:apply-templates>
          </xsl:copy>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

<xsl:template match="EntityAssignedNumber">
    <xsl:param name="pos"/>
    <xsl:copy>
        <xsl:value-of select="/root/RemittanceInformation[1]/EntityAssignedNumber + $pos - 1"/>
    </xsl:copy>
</xsl:template>

我从 S/O 上超级有用的 martin-honnen 获得了这段代码,但它没有做的一件事是,当 RemittanceInformation 类型的节点没有 IndividualRemmittance 时,它​​根本不会复制它.

我想要的是当有一个 IndividualRemmittance 时,将它们组合在一起以便没有重复项(代码已经这样做了),但是当它遇到没有 IndividualRemmittance 的 RemittanceInformation 时,它应该正常复制它。

我需要改变什么才能做到这一点?

【问题讨论】:

  • 您是否尝试过理解代码?您怀疑需要修改哪一部分?你有没有尝试过?你的尝试结果如何?
  • 好吧,分组是由“IndividualREmittance/ExchangeAssignedPolicyID”完成的,但我不知道当没有 IndividualRemittance 存在时,我应该如何...“不”组。我不知道如何设置这个条件。
  • 好的,所以您不想将它们分组,而是复制它们。这是两件事。换一种说法,您要做的是 1) 将它们排除在分组之外,以及 2) 分别复制它们。
  • 好吧,他们已经被排除在外了,因为他们没有 IndividualRemittance 节点,这很好。现在要在保持编号约定的同时单独复制它们,我不知道如何。你呢?
  • 我愿意,但我希望你能建立联系。 :) 复制节点的 XSL 元素是 &lt;xsl:copy&gt;。这并不是那么困难。

标签: xml xslt


【解决方案1】:

感谢 Tomalak 的帮助,我成功了! :D

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output indent="yes"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="root">
     <xsl:variable name="countComp" select="count(//RemittanceInformation[not(IndividualRemittance)])"/>
     <xsl:copy>
      <xsl:for-each select="RemittanceInformation[not(IndividualRemittance)]">
        <xsl:variable name="pos" select="position()"/>
        <xsl:copy>
          <xsl:apply-templates select="@* | node()">
            <xsl:with-param name="pos" select="$pos"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:for-each>

      <xsl:for-each-group select="RemittanceInformation" group-by="IndividualRemittance/ExchangeAssignedPolicyID">
        <xsl:copy>
          <xsl:variable name="pos" select="position()"/>
          <xsl:apply-templates select="EntityAssignedNumber, IndividualRemittance, current-group()/RemittanceDetail">
            <xsl:with-param name="pos" select="$pos+$countComp"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="EntityAssignedNumber">
    <xsl:param name="pos"/>
    <xsl:copy>
      <xsl:value-of select="/root/RemittanceInformation[1]/EntityAssignedNumber + $pos - 1"/>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

【讨论】:

  • 这是我使用的硬编码数字。正确地,它应该是一个变量。我马上更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
相关资源
最近更新 更多