Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Having trouble with siblings

by Anonymous Monk
on Jun 30, 2013 at 14:34 UTC ( [id://1041599]=note: print w/replies, xml ) Need Help??


in reply to Having trouble with siblings

Same tricks from Re: Get Node Value from irregular XML (xpather.pl)  "//header[ contains(.,'rationale') ]/* "
xmllint.exe --xpath " //header[ contains(.,'rationale') ]/l /preceding +-sibling::* " fudge xmllint.exe --xpath " //header[ contains(.,'rationale') ]/child::text( +) " fudge
where fudge is your data part=1 part=2
<fudge> <root> <part> <sect> <header> 1. Purpose and rationale <p>purpose 1</p> <p>Purpose 2</p> <p>purpose 3</p> <l> <li>purpose list 1</li> <li>list2</li> </l> </header> </sect> </part> </root> <root> <part> <sect> <header> 2. Purpose and rationale <p>2 purpose 1</p> <p>2 Purpose 2</p> <p>2 purpose 3</p> <l> <li>2 purpose list 1</li> <li>2 list2</li> </l> <p>2. Some other heading</p> <p>2 content 1</p> <p>2 content 2</p> </header> </sect> </part> </root> </fudge>

The two queries combined, with the results

xmllint.exe --xpath " //header[ contains(.,'rationale') ]/child::text( +) | //header[ contains(.,'rationale') ] /l/preceding-sibling::* " f +udge 1. Purpose and rationale <p>purpose 1</p> <p>Purpose 2</p> <p>purpose 3</p> 2. Purpose and rationale <p>2 purpose 1</p> <p>2 Purpose 2</p> <p>2 purpose 3</p>

Replies are listed 'Best First'.
Re^2: Having trouble with siblings
by Anonymous Monk on Jun 30, 2013 at 15:34 UTC

    Aha, the node test node() will select all types of nodes, even text() nodes

    • so find a <header> which contains 'rationale'
    • and select every child node of that header
    • and filter this nodeset for nodes which have following-sibling with tagname  <l> or which have tagname  <l>
    xmllint.exe --xpath " //header[ contains(.,'rationale') ]/node()[ fol +lowing-sibling::l or self::l] " fudge 1. Purpose and rationale <p>purpose 1</p> <p>Purpose 2</p> <p>purpose 3</p> <l> <li>purpose list 1</li> <li>list2</li> </l> 2. Purpose and rationale <p>2 purpose 1</p> <p>2 Purpose 2</p> <p>2 purpose 3</p> <l> <li>2 purpose list 1</li> <li>2 list2</li> </l>

    Naturally if there are more than one  <l> you can limit to first <l> with position()  xmllint.exe --xpath " //header[ contains(.,'rationale') ]/node()[ following-sibling::l[ position()=1 ] or self::l[ position()=1 ]  ]   " fudge

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1041599]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-20 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found