xpath的属性不存在时通过属性选择(xpath select by attribute when

2019-08-03 20:36发布

简短的问题:我想选择不匹配的属性的所有节点,但也属性不存在(??)(@type =“X”!)。 目前,我得到任何回报,因为其他节点没有属性可言。

背景:我有一些XML。 需要注意的是一个具有类型=“功能”,但所有其他人都没有“类型” ATTR。

<image type="feature"><description>X</description><url>media/designer_glass_tile_04.jpg</url><height></height><width/></image>
<image><description>Designer Glass 05</description><url>media/designer_glass_tile_05.jpg</url><height></height><width/></image>
<image><description>Designer Glass 06</description><url>media/designer_glass_tile_06.jpg</url><height></height><width/></image>
<image><description>Designer Glass 07</description><url>media/designer_glass_tile_07.jpg</url><height></height><width/></image>
<image><description>Designer Glass 08</description><url>media/designer_glass_tile_08.jpg</url><height></height><width/></image>

而一个XSL样式:

        <div id="gallery">
            <div id="feature" >
                <xsl:apply-templates select="image[@type='feature']"/>
            </div>
            <div id="thumbs">
                <xsl:apply-templates select="image[@type!='feature']"/>
            </div>
        </div>

Answer 1:

下面的代码将选择所有节点,其中type属性不存在:

select="image[not(@type)]"

在代码中添加此逻辑。



Answer 2:

尝试使用not()代替!=在谓语...

   <div id="gallery">
        <div id="feature" >
            <xsl:apply-templates select="image[@type='feature']"/>
        </div>
        <div id="thumbs">
            <xsl:apply-templates select="image[not(@type='feature')]"/>
        </div>
    </div>


文章来源: xpath select by attribute when attribute not present
标签: xml xslt xpath