XSLT检查其中一个兄弟元素是否包含某些参数

时间:2014-08-21 22:59:35

标签: xml xslt properties filter

我有XML列出我的菜单项。问题是,它列出了所有项目,无论其语言如何,我需要过滤掉所有不属于当前语言的项目,这些项目存储在$language变量中。

这是我的XSLT,它应该过滤掉不属于当前语言的菜单项(它也应该过滤隐藏的项目):

<xsl:apply-templates select="page[not(types/type = 'hidden' or types/type != $language)]"/>

目前,只有关于元素的完成,这可能是因为我的Home项目除了index之外还有en

那么,如果元素中没有包含当前语言的正确type而忽略其他属性,我该如何过滤掉元素?

这是我的XML:

<navigation>
    <page handle="en" id="1">
        <name>Home</name>
        <types>
            <type>en</type>
            <type>index</type>
        </types>
    </page>
    <page handle="he" id="2">
        <name>Home HE</name>
        <types>
            <type>he</type>
        </types>
    </page>
    <page handle="about" id="3">
        <name>About</name>
        <types>
            <type>en</type>
        </types>
    </page>
</navigation>

2 个答案:

答案 0 :(得分:0)

要比较元素值,请使用text()

<xsl:apply-templates select="page[not(types/type/text() = 'hidden' or types/type/text() != $language)]"/>

答案 1 :(得分:0)

你的问题是not()函数,它不会以你尝试使用它的方式工作。 not()接受数字,字符串或节点集,应用boolean()函数,然后返回逆。 not()显然不会反转布尔表达式。见http://www.w3schools.com/xpath/xpath_functions.asp#boolean

您可以通过将逆运算符分配到操作数并使用&#34;和&#34;来实现您想要的效果:

<xsl:apply-templates select="page[types/type != 'hidden' and types/type = $language]" />