XSLT 1.0转换使用变量删除重复项

问题描述 投票:0回答:1

由于多种原因,我需要在XSLT 1.0中派生一个变量,该变量可以在变换期间重复使用,它可以收集重复条目的唯一列表。

输入数据在XSLT中生成为变量“portlist”:

<plist> 
  <p>12345</p>
  <p>12345</p>
  <p>9876</p>
  <p>12345</p>
<plist>

在我的XSLT模板中,我需要一个变量“reducedList”,在转换中可以多次重复使用。如何在XSLT中生成一个新的变量“reducedList”,看起来像

<plist> 
  <p>12345</p>
  <p>9876</p>
<plist>

我找到了几个例子,但必须承认我无法弄清楚。

我的xslt模板看起来像

<xsl:template match="stage">

   <xsl:variable name="portlist" > <!-- returns a sorted list of all ports -->
       <plist>
          <xsl:for-each select="provider/server/QMGR"><!-- input from XML -->
             <xsl:sort select="."/>
             <p><xsl:value-of select="./@port"/></p>
           </xsl:for-each>
        </plist>
    </xsl:variable>

    <!-- here i need to derive the new variable reducedList  -->

    <!-- more code using reducedList follows here -->
</xsl:template>
xslt-1.0
1个回答
0
投票
<xsl:variable name="portlist">
  <plist>
    <p>12345</p>
    <p>12345</p>
    <p>9876</p>
    <p>12345</p>
  </plist>
</xsl:variable>

<xsl:variable name="reducedList">
  <plist>
    <xsl:copy-of select="ext:node-set($portlist)/plist/p[not(text() = preceding-sibling::p/text())]"/>
  </plist>
</xsl:variable>

其中extnode-set()的扩展命名空间,例如xmlns:ext="urn:schemas-microsoft-com:xslt"

© www.soinside.com 2019 - 2024. All rights reserved.