当属性不存在时,xpath 按属性选择 [英] xpath select by attribute when attribute not present

查看:47
本文介绍了当属性不存在时,xpath 按属性选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

背景:我有一些 XML.请注意,一个具有 type="feature" 但所有其他都没有 'type' attr.

Xmedia/designer_glass_tile_04.jpg

和 XSL 样式:

 

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

解决方案

以下代码将选择所有不存在 type 属性的节点:

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

在您的代码中添加此逻辑.

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

Background: I have some XML. Note that one has type="feature" but all others have no '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:

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

解决方案

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

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

Add this logic in your code.

这篇关于当属性不存在时,xpath 按属性选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆