xml 当属性不存在时,xpath 按属性选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13807499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xpath select by attribute when attribute not present
提问by Gabe Rainbow
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.
简短的问题:我想选择所有不匹配属性(@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>
回答by RATHI
The following code will select all nodes where type attribute doesn't exist:
以下代码将选择所有类型属性不存在的节点:
select="image[not(@type)]"
Add this logic in your code.
在您的代码中添加此逻辑。
回答by Daniel Haley
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>

