在一组节点上进行XSL测试?

时间:2011-05-19 19:41:58

标签: xslt xpath

我有一组带有type属性

的“文件”节点
<files>
    <file type="main">asdf</file>
    <file type="main_en">asdf</file>
    <file type="main_fr">asdf</file>
    <file type="pdf">asdf</file>
</files>

如果其中一个节点至少有一个以“main”开头的属性,我该如何检查文件集。 我想的是:

<xsl:when test="contains(string(files/file[@type]),'main')">

但我知道的所有函数或测试似乎只针对特定节点而不是一组节点。 我宁愿避免使用每种类型的解决方案。

3 个答案:

答案 0 :(得分:2)

  

如何检查文件集   其中一个节点至少有1个   属性以“main”开头。

使用

boolean(/*/file[@*[starts-with(., 'main')]])

当至少有一个true()元素是XML文档顶部元素的子元素并且至少有一个属性,其字符串值以字符串file

"main"属性(test<xsl:if>)或谓词中使用时,对<xsl:when>函数的引用不是必需的,可能被省略

boolean()

答案 1 :(得分:1)

<xsl:when test="files/file[contains(@type, 'main')]">

或者,甚至更好:

<xsl:when test="files/file[starts-with(@type, 'main')]">

答案 2 :(得分:0)

不知道你的真实目的,但这个例子可能对你有帮助:

 <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/files">
        <xsl:if test="file[contains(@type,'main')]">
            There is at least one file element with 'main' in @type
        </xsl:if>
  </xsl:template>


</xsl:stylesheet>