Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath select by attribute when attribute not present

Tags:

xml

xslt

xpath

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> 
like image 204
Gabe Rainbow Avatar asked Dec 10 '12 19:12

Gabe Rainbow


People also ask

What is the meaning of '/' in XPath?

0 votes. Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.

What is the * indicates in XPath?

By adding '//*' in XPath you would be selecting all the element nodes from the entire document. In case of the Gmail Password fields, .//*[@id='Passwd'] would select all the element nodes descending from the current node for which @id-attribute-value is equal to 'Passwd'.

How use attribute for contain in XPath?

Using the XPath contains() function, we can extract all the elements on the page that match the provided text value. Here, tag: tag is the name of the tag that contains the specific word. word: In this case, the word refers to the text that must be discovered in a specific string.


2 Answers

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

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

Add this logic in your code.

like image 131
RATHI Avatar answered Oct 04 '22 04:10

RATHI


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> 
like image 27
Daniel Haley Avatar answered Oct 04 '22 02:10

Daniel Haley