无法获取模板匹配中当前节点的详细信息 'L'

Not able to get details of current node in template match 'L'

这个 xslt 工作得很好,但我需要在生成的输出中的 L 节点下获取更多详细信息,我在形成测试、testone 节点时遇到问题。我尝试的解决方案是 <xsl:apply-templates select="@*|node()|SL[@id = $ref]" mode="Loutput"/> 在这一行中包括 node() 但在这种情况下输出将是错误的。

输入xml如下

<root>
  <L Id="L1">
    <test>ed</test>
    <testone><testtwo>ed</testtwo></testone>    
    <SL id="L1S1">
      <check>
        <AId>1</AId>
      </check>
      <MD>
        <UnitNumber>1</UnitNumber>
      </MD>
    </SL>
    <SL id="L1S2">
      <check>
        <AId>2</AId>
      </check>
      <MD>
        <UnitNumber>2</UnitNumber>
      </MD>
    </SL>
  </L>
  <cp>
    <current>
      <Amt>20154.00</Amt>
    </current>
    <pi>
      <pit ref="L1S1">
        <value>123</value>
      </pit>
      <pit ref="L1S2">
        <value>1232</value>
      </pit>
    </pi>
  </cp>
</root>

预期输出应为:

<root>
  <L Id="L1">
    <test>ed</test>
    <testone><testtwo>ed</testtwo></testone>    
    <SL id="L1S1">
      <check>
        <AId>1</AId>
      </check>
      <MD>
        <UnitNumber>1</UnitNumber>
      </MD>
    </SL>
    <pit ref="L1S1">
      <value>123</value>
    </pit>
  </L>
  <L Id="L1">
    <test>ed</test>
    <testone><testtwo>ed</testtwo></testone>    
    <SL id="L1S2">
      <check>
        <AId>2</AId>
      </check>
      <MD>
        <UnitNumber>2</UnitNumber>
      </MD>
    </SL>
    <pit ref="L1S2">
      <value>1232</value>
    </pit>
  </L>
</root>

这个 xslt 适用于我的所有场景。


    <xsl:key name="SLkey" match="SL" use="@id"/>
    <xsl:key name="pitKey" match="pit" use="generate-id()"/>

    <xsl:template match="root">
      <xsl:copy>
        <xsl:apply-templates select="@*|.//pit"/>
      </xsl:copy>
    </xsl:template>
  
    <xsl:template match="pit">
      <xsl:choose>
        <!-- Is there an SL node match?  -->
        <xsl:when test="key('SLkey', @ref)[1]">
          <xsl:apply-templates select="key('SLkey', @ref)[1]/.." mode="Loutput">
            <xsl:with-param name="ref" select="@ref"/>
            <xsl:with-param name="pitID" select="generate-id(.)"/>
          </xsl:apply-templates>
        </xsl:when>
        <!-- Use the first L in the document. -->
        <xsl:otherwise>
          <xsl:apply-templates select="//L[1]" mode="Loutput">
            <xsl:with-param name="ref" select="@ref"/>
            <xsl:with-param name="pitID" select="generate-id(.)"/>
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
  
    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>
  
    <!--  **********************  -->
    <!--   Loutput mode templates  -->
    <!--  **********************  -->
    <xsl:template match="node()|@*" mode="Loutput">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*" mode="Loutput"/>
      </xsl:copy>
    </xsl:template>
  
    <xsl:template match="L" mode="Loutput">
      <xsl:param name="ref"/>
      <xsl:param name="pitID"/>
      <xsl:copy>
        <xsl:apply-templates select="@*|SL[@id = $ref]" mode="Loutput"/>
        <xsl:apply-templates select="key('pitKey', $pitID)" mode="Loutput"/>
      </xsl:copy>
    </xsl:template>

这是一种方法。

<xsl:key name="SLkey" match="SL" use="@id"/>
<xsl:key name="pitKey" match="pit" use="generate-id()"/>

<xsl:template match="root">
  <xsl:copy>
    <xsl:apply-templates select="@*|.//pit"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="pit">
  <xsl:choose>
    <!-- Is there an SL node match?  -->
    <xsl:when test="key('SLkey', @ref)[1]">
      <xsl:apply-templates select="key('SLkey', @ref)[1]/.." mode="Loutput">
        <xsl:with-param name="ref" select="@ref"/>
        <xsl:with-param name="pitID" select="generate-id(.)"/>
      </xsl:apply-templates>
    </xsl:when>
    <!-- Use the first L in the document. -->
    <xsl:otherwise>
      <xsl:apply-templates select="//L[1]" mode="Loutput">
        <xsl:with-param name="ref" select="@ref"/>
        <xsl:with-param name="pitID" select="generate-id(.)"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

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

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

<xsl:template match="L" mode="Loutput">
  <xsl:param name="ref"/>
  <xsl:param name="pitID"/>
  <xsl:copy>
    <xsl:apply-templates select="@*|node()[local-name() != 'SL']" mode="Loutput"/>
    <xsl:apply-templates select="SL[@id = $ref]" mode="Loutput"/>
    <xsl:apply-templates select="key('pitKey', $pitID)" mode="Loutput"/>
  </xsl:copy>
</xsl:template>