XSLT - 检查序列的所有节点是否匹配一组值

XSLT - check if all nodes of a sequence match a set of values

使用 XSLT v2.0,我如何检查所有 selected 节点的文本是否匹配某些参考值?

比如我select所有H1个节点。我想确保它们都等于 "The title" 或 "A heading".

我一直在尝试为此创建一个函数:

<xsl:function name="is-valid" as="xs:boolean">
    <xsl:param name="seq" as="item()*" />
    <xsl:for-each select="$seq">
            <xsl:if test="not(matches(current()/text(),'The title|A heading'))">
                <!-- ??? -->           
            </xsl:if>
    </xsl:for-each>
</xsl:function>

我不认为这是 XSLT 的方式,但我找不到如何做到这一点。

有什么提示吗?

XSLT 2.0 有一个 every..satisfies 结构可以在此处提供帮助:

<xsl:function name="e:is-valid" as="xs:boolean">
  <xsl:param name="s" as="item()*" />
  <xsl:value-of select="every $i in $s satisfies $i=('The title', 'A heading')"/>
</xsl:function>

这是一个完整的例子:

XML

<?xml version="1.0" encoding="UTF-8"?>
<r>
  <h1>Wrong title</h1>
  <h1>The title</h1>
  <h1>A heading</h1>
</r>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:e="http://example.com/f">

  <xsl:template match="/">
    <xsl:message>
      <xsl:value-of select="e:is-valid(//h1)"/>
    </xsl:message>
  </xsl:template>

  <xsl:function name="e:is-valid" as="xs:boolean">
    <xsl:param name="s" as="item()*" />
    <xsl:value-of select="every $i in $s satisfies $i=('The title','A heading')"/>
  </xsl:function>

</xsl:stylesheet>

只需使用这个简单的 XPath 表达式 -- the double negation law:

not(h1[not(. = ('The title','A heading'))])

作为演示,给出与@kjhughes 的回答相同的 XML 文档:

<r>
  <h1>Wrong title</h1>
  <h1>The title</h1>
  <h1>A heading</h1>
</r>

此 XSLT 2.0 转换:

<xsl:stylesheet version="20"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

  <xsl:template match="/*">
    <xsl:sequence select="not(h1[not(. = ('The title','A heading'))])"/>
  </xsl:template>
</xsl:stylesheet>

产生想要的正确结果:

false

这可以在 XPath 1.0 中用于确定节点集 $ns1 的所有字符串值是否都在另一个节点集 $ns2 的字符串值中:

not(not($ns1[. = $ns2]))

这是与 XSLT 2.0 /XPath 2.0 解决方案等效的 XPath 1,0 / XSLT 1.0:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:param name="pValues">
   <v>The title</v>
   <v>A heading</v>
 </xsl:param>

 <xsl:variable name="vValues" select="document('')/*/xsl:param[@name='pValues']/*"/>

  <xsl:template match="/*">
    <xsl:value-of select="not(h1[not(. = $vValues)])"/>
  </xsl:template>
</xsl:stylesheet>