首页 > 解决方案 > XSLT 1.0 转换使用变量删除重复项

问题描述

由于几个原因,我需要在 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

解决方案


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

ext您的扩展名称空间在哪里node-set(),例如xmlns:ext="urn:schemas-microsoft-com:xslt".


推荐阅读