xpath select by attribute when attribute not present

XmlXsltXpath

Xml Problem Overview


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>

Xml Solutions


Solution 1 - Xml

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

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

Add this logic in your code.

Solution 2 - Xml

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGabe RainbowView Question on Stackoverflow
Solution 1 - XmlRATHIView Answer on Stackoverflow
Solution 2 - XmlDaniel HaleyView Answer on Stackoverflow