Is there a way to test for child nodes?

17 views
Skip to first unread message

Wayne Brisette

unread,
Apr 15, 2014, 10:46:45 AM4/15/14
to nokogi...@googlegroups.com
I have a DITA map (XML document), where I'm trying to visually build a text file. 

Here's an extract: 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE map  PUBLIC "-//OASIS//ELEMENTS DITA Map//EN" "map.dtd">
     ditaarch:DITAArchVersion="1.2"
     id="lou1375305364946"
     xml:lang="en-us">
    <title>app note</title>
    
    <topicref keys="lou1367512903038" href="lou1367512903038.xml" type="reference">
      <topicref keys="lou1387324160222" href="lou1387324160222.xml" type="reference">
         <topicref keys="lou1397064605122" href="lou1397064605122.xml" type="reference"/>
      </topicref>
      <topicref keys="lou1387229890414" href="lou1387229890414.xml" type="reference">
         <topicref keys="lou1375985424101" href="lou1375985424101.xml" type="task">
            <topicref keys="lou1375999330446" href="lou1375999330446.xml" type="reference">
               <topicref keys="lou1375998148445" href="lou1375998148445.xml" type="reference">
                  <topicref keys="lou1376001561381" href="lou1376001561381.xml" type="reference"/>
                  <topicref keys="lou1376001612107" href="lou1376001612107.xml" type="task"/>
                  <topicref keys="lou1376001839463" href="lou1376001839463.xml" type="reference"/>
               </topicref>
               <topicref keys="lou1376001962698" href="lou1376001962698.xml" type="reference"/>
               <topicref keys="lou1376083974509" href="lou1376083974509.xml" type="reference"/>
               <topicref keys="lou1376002273220" href="lou1376002273220.xml" type="reference"/>
               <topicref keys="lou1376002339131" href="lou1376002339131.xml" type="reference"/>
               <topicref keys="lou1376002410143" href="lou1376002410143.xml" type="reference"/>
            </topicref>
         </topicref>
      </topicref>


Is there a way to test if there is a child node? Then parse those before moving down the tree? 

Wayne


Mike Dalessio

unread,
Apr 15, 2014, 11:11:07 PM4/15/14
to nokogiri-talk

Hi Wayne,

Thanks for asking this question.

I'm not totally sure what you're asking. Do you have a code (or pseudo-code) snippet that explains what you're trying to do?

>
> Wayne
>
>
> --
> You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
> To post to this group, send email to nokogi...@googlegroups.com.
> Visit this group at http://groups.google.com/group/nokogiri-talk.
> For more options, visit https://groups.google.com/d/optout.

Wayne Brissette

unread,
Apr 16, 2014, 5:59:24 AM4/16/14
to nokogi...@googlegroups.com

On Apr 15, 2014, at 22:11, Mike Dalessio <mike.d...@gmail.com> wrote:

I'm not totally sure what you're asking. Do you have a code (or pseudo-code) snippet that explains what you're trying to do?



Hi Mike:

If I use  /topicref I get back my entire tree. 

For a lot of things this is fine, in fact it’s preferred. For example I loop through my topics refs and then use /topicref/@href to open each xml file. When I produce the output it’s in order of the map (think of the ditamap as a table of contents first topic = page 1, second topic = page 2, etc.). But what people using the script want to know is what the hierarchy looks like, so in this example: 

 <topicref keys="lou1387324160222" href="lou1387324160222.xml" type="reference">
          <topicref keys="lou1397064605122" href="lou1397064605122.xml" type="reference"/>
 </topicref>
<topicref keys="lou1387229890414" href="lou1387229890414.xml" type="reference">
          <topicref keys="lou1375985424101" href="lou1375985424101.xml" type="task">

if I was building it, I would like to know that lou1397064605122.xml is a child of lou1387324160222.xml. 

So when I build out the text version of the map it would look something like this: 


topic 1 title (lou1387324160222.xml)
     topic 2 title (lou1397064605122.xml)
topic 3 title (lou1387229890414.xml)
      topic 4 title (lou1375985424101.xml)

Note: Titles here have to come from the actual XML files themselves since there is no indication in this map of what the title is… 

I know if I build a full-blown XSLT template I can do some testing to see what nodes are parents and what nodes are children (although I do have to admit XSLT and XPATH really aren’t my strong points). 

What I’m really trying to do at this point is parse this map and rebuild it knowing the hierarchy so I can add some number of spaces for each child of a parent. 

My code right now is very basic because all I’m doing right now is reading the entire list of topicrefs using /topicref   via  xpath(“/topicref”).text

I’m just not sure if there’s an easy way to walk the tree and determine is this item a parent, a child, or a child of a child.

Make sense? 

Wayne
Reply all
Reply to author
Forward
0 new messages