当形成的节点集中有相同的键时出现问题

Having issue when there are identical keys in nodeset formed

我的输入 xml 如下,当节点集中存在多个相同的键时,我遇到了问题,它没有形成正确的输出结构。我使用键通过 ref 属性形成节点集。我正在使用这些 <SL id="L1S1"><pit ref="L1S1"> 来形成节点集。 SL节点(即<SL id="L1S1">)的id属性是使用<L Id="L1">中的“L1”形成的。坑节点(即<pit ref="L1S1">)的ref属性也是使用<L Id="L1">中的“L1”形成的。

如果我没有多个具有相同参考编号的坑节点,这个解决方案就可以正常工作。我正在使用 xslt1.0,当在节点集中发现多个相同的键时,我遇到了问题。

Input xml as below
<root>
  <L Id="L1">
    <test>ed</test>
    <SL id="L1S1">
      <check>
        <AId>1</AId>
      </check>
    </SL>
    <SL id="L1S2">
      <check>
        <AId>2</AId>
      </check>
    </SL>
  </L>
  <cp>
    <current>
      <Amt>20154.00</Amt>
    </current>
    <pi>
      <pit ref="L1S1">
        <value>123</value>
      </pit>
      <pit ref="L1S1">
        <value>234</value>
      </pit>
      <pit ref="L1S2">
        <value>1232</value>
      </pit>
    </pi>
  </cp>
</root>

预期输出应为:

<root>
  <L Id="L1">
    <SL id="L1S1">
      <check>
        <AId>1</AId>
      </check>
    </SL>
    <pit ref="L1S1">
      <value>123</value>
    </pit>
  </L>
  <L Id="L1">
    <SL id="L1S1">
      <check>
        <AId>1</AId>
      </check>
    </SL>
    <pit ref="L1S1">
      <value>234</value>
    </pit>
  </L>
  <L Id="L1">
    <SL id="L1S2">
      <check>
        <AId>2</AId>
      </check>
    </SL>
    <pit ref="L1S2">
      <value>1232</value>
    </pit>
  </L>
</root>

  <xsl:key name="pit-SL" match="pit" use="@ref" />
  <xsl:key name="pit-L"  match="pit" use="substring(@ref,1,2)" />

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

  <xsl:template match="L[SL]">
    <xsl:apply-templates select="SL"/>
  </xsl:template>

  <xsl:template match="SL">
    <L>
      <xsl:copy-of select="parent::L/@Id"/>
      <xsl:copy>
        <xsl:copy-of  select="@id"/>
        <xsl:apply-templates select="node()"/>
        <xsl:copy-of select="key('pit-SL',@id)"/>
      </xsl:copy>
    </L>
  </xsl:template>

  <xsl:template match="L[not(SL)]">
    <xsl:apply-templates select="key('pit-L',@Id)">
      <xsl:with-param name="L" select="."/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="pit">
    <xsl:param name="L"/>
    <L>
      <xsl:copy-of select="$L/@Id"/>
      <xsl:copy-of select="."/>
    </L>
  </xsl:template>

  <xsl:template match="cp"/>

AFAICT,你可以这样做:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="pit" match="pit" use="@ref" />

<xsl:template match="/root">
    <xsl:copy>
        <xsl:for-each select="L">
            <xsl:variable name="Id" select="@Id" />
            <xsl:for-each select="SL">
                <xsl:variable name="SL" select="." />
                <xsl:for-each select="key('pit', @id)">
                    <L>
                        <xsl:copy-of select="$Id"/>
                        <xsl:copy-of select="$SL"/>
                        <xsl:copy-of select="."/>
                    </L>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>