Xsl: Copy For Each while ignoring all nodes that d

2019-09-14 06:07发布

问题:

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

I got this code from the super helpful martin-honnen on S/O, but one thing it doesn't do is, when a node of type RemittanceInformation doesn't have an IndividualRemmittance, it doesn't copy it at all.

What I would like is when there is an IndividualRemmittance, to group them together so there are no duplicates (The code does that already), but when it runs into a RemittanceInformation without an IndividualRemmittance, it should just copy it across normally.

What do I need to alter to make that happen?

回答1:

Thanks to Tomalak's help, I made it work! :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>


标签: xml xslt