XSLT:筛选出除给定列表以外的结果

时间:2018-11-28 21:41:33

标签: xslt

我想过滤掉列表中的某些元素。

我有这个XML:

<catalog>
   <product>
      <code>Y17231</code>
      <pname>Test 1</pname>
   </product>
   <product>
      <code>Y19232</code>
      <pname>Test 2</pname>
   </product>
   <product>
      <code>Y18333</code>
      <pname>Test 3</pname>
   </product>
</catalog>

目前,我正在过滤掉所有不是以Y19 *代码开头的元素。

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

    <xsl:strip-space elements="*" />

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

    <xsl:template match="product[not(matches(code, 'Y[^X]?19.*[^V]$'))]" />

</xsl:stylesheet>

这是可行的,xsltransform示例,示例输出:

<catalog>
   <product>
      <code>Y19232</code>
      <pname>Test 2</pname>
   </product>
</catalog>

目标:

我想用一个功能扩展它,在这里我可以定义一些代码,然后将它们过滤掉(它们将包含在结果中)。

<xsl:param name="include-this" as="xs:string*" select="'Y18333','Y16222','Y12333'"/>

所以输出应该是这样:

<catalog>
   <product>
      <code>Y19232</code>
      <pname>Test 2</pname>
   </product>
   <product>
      <code>Y18333</code>
      <pname>Test 3</pname>
   </product>
</catalog>

您能帮我实现我的目标吗?

1 个答案:

答案 0 :(得分:1)

我认为您想使用match="product[not(code = $include-this) and not(matches(code, 'Y[^X]?19.*[^V]$'))]"