当attibute不存在时,通过属性选择xpath

时间:2021-11-13 22:44:23

Short question: I would like to select all nodes that do not match an attribute (@type !='x') but also attribute doesnt exist (??). Currently, im getting nothing back, because the other nodes have no attribute at all.

简短的问题:我想选择所有不匹配属性的节点(@type !='x'),但也不存在属性(??)目前,我什么也没有得到,因为其他节点根本没有属性。

Background: I have some XML. Note that one has type="feature" but all others have no 'type' attr.

背景:我有一些XML。请注意,其中一个有type="feature",但其他的都没有'type' 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>

And a XSL style:

和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>

Thanks for the time.

谢谢你的时间。

2 个解决方案

#1


57  

The following code will select all nodes where type attribute doesn't exist:

下面的代码将选择类型属性不存在的所有节点:

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

Add this logic in your code.

在代码中添加此逻辑。

#2


11  

Try using not() instead of != in the predicate...

在谓词中尝试使用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>

#1


57  

The following code will select all nodes where type attribute doesn't exist:

下面的代码将选择类型属性不存在的所有节点:

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

Add this logic in your code.

在代码中添加此逻辑。

#2


11  

Try using not() instead of != in the predicate...

在谓词中尝试使用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>