Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于检查变量是否属于元素集的XSLT表达式_Xslt - Fatal编程技术网

用于检查变量是否属于元素集的XSLT表达式

用于检查变量是否属于元素集的XSLT表达式,xslt,Xslt,我有这样的代码: <xsl:if test="$k='7' or $k = '8' or $k = '9'"> Ty:)XSLT/XPath 1.0: <!-- a space-separated list of valid values --> <xsl:variable name="list" select="'7 8 9'" /> <xsl:if test=" contains( concat(' ', $list, ' ')

我有这样的代码:

  <xsl:if test="$k='7' or $k = '8' or $k = '9'">
Ty:)

XSLT/XPath 1.0:

<!-- a space-separated list of valid values -->
<xsl:variable name="list" select="'7 8 9'" />

<xsl:if test="
  contains( 
    concat(' ', $list, ' '),
    concat(' ', $k, ' ')
  )
">
  <xsl:value-of select="concat('Item ', $k, ' is in the list.')" />
</xsl:if>

如果需要,您可以使用其他分离器

在XSLT/XPath 2.0中,您可以执行以下操作:

<xsl:variable name="list" select="fn:tokenize('7 8 9', '\s+')" />

<xsl:if test="fn:index-of($list, $k)">
  <xsl:value-of select="concat('Item ', $k, ' is in the list.')" />
</xsl:if>

如果可以使用文档结构定义列表,则可以执行以下操作:

<!-- a node-set defining the list of currently valid items -->
<xsl:variable name="list" select="/some/items[1]/item" />

<xsl:template match="/">
  <xsl:variable name="k" select="'7'" />

  <!-- test if item $k is in the list of valid items -->
  <xsl:if test="count($list[@id = $k])">
    <xsl:value-of select="concat('Item ', $k, ' is in the list.')" />
  </xsl:if>
</xsl:template>

如果处理器支持XPath 2.0,则可以将
$k
与如下序列进行比较:

<xsl:if test="$k = (7, 8, 9)">

在这种特殊情况下,您甚至可以使用范围运算符:

<xsl:if test="$k = (7 to 9)">

不需要显式类型转换。使用Saxon HE 9.8.0.12N(XSLT 3.0)进行测试

XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <node>5</node>
    <node>6</node>
    <node>7</node>
    <node>9</node>
    <node>10</node>
    <node>79</node>
    <node>8</node>
</root>

5.
6.
7.
9
10
79
8.
样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="root">
        <xsl:copy>
            <xsl:apply-templates select="node"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node">
        <xsl:variable name="k" select="text()"/>
        <xsl:if test="$k = (7 to 9)">
            <xsl:copy-of select="."/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <node>7</node>
   <node>9</node>
   <node>8</node>
</root>

7.
9
8.

Ty m8。对于像我这样的简单场景来说,这太过分了,但是对于长列表来说,这是可以的。。。我更希望xpath有某种形式的集成解决方案……而不是xpath 1.0——当您无法使用节点集来解决问题时,您只能使用字符串函数或某些扩展函数。XPath2.0的序列在任何情况下都使思考变得更容易。不管你是否用引号把数字括起来,这都不会有什么区别,至少对撒克逊人来说是这样。对于非数字字符,您当然需要引号。在我的例子中,
$k
中的值可能有前导零。在我提供的示例中,没有表示。因此,我将我的值视为字符串,以避免出现不期望的行为。
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <node>7</node>
   <node>9</node>
   <node>8</node>
</root>