www.webdeveloper.com
Results 1 to 6 of 6

Thread: Looping throught XML childnodes without JavaScript

  1. #1
    Join Date
    Apr 2010
    Posts
    9

    Looping throught XML childnodes without JavaScript

    Hi there

    A quick one (I think).
    I am kind of new to XML and I'm kind of stuck on something that may be trivial to most of you.
    I have something like this:

    eg 1:
    ***********
    <book>
    <table_of_content>List of topics:</table_of_content>
    <content>
    <title>title one</title>
    <title>title two</title>
    <title>title three</title>
    <title>title four</title>
    </content>
    </book>
    ***********
    How will I display on the site page something like
    ____
    List of topics:
    1. title one
    2. title two
    3. title three
    4. title four
    ____

    I mean how would I do something like looping through the elements (what I am mostly interested is displaying the values of elements with the same name, i.e. title,title,title,etc) but using only XML, XSD, XSLT/XPath, without using JavaScript or Pearl or similar?

    eg 2:

    **********
    <club_members>
    <name>John Smith</name>
    <email>john.smith@mail.com</email>
    <name>Joe Smith</name>
    <email>joe.smith@mail.com</email>
    <name>Bob Smith</name>
    <email>bob.smith@mail.com</email>
    <name>Black Smith</name>
    <email>black.smith@mail.com</email>
    <name>Lock Smith</name>
    <email>lock.smith@mail.com</email>
    </club_members>

    **********
    Here, how do you display on page the values of all occurances of name and email elements, as in:

    John Smith / john.smith@mail.com
    Joe Smith / joe.smith@mail.com
    Bob Smith / bob.smith@mail.com
    .
    .
    .

    ???

    In my naivity and misunderstanding of how XML and XSLT/XPath works I thought something like
    <xsl:for-each select="club_members">
    <xsl:value-of select="name"/>
    <xsl:value-of select="email"/>
    </xsl:for-each>
    would actually loop through the children of some parent element.

    Would you,guys, be so kind and put toghether a tiny combination of the three (XML, XSLT/XPath, XSD) that might show me how these work toghegher towards the result I mentioned before? But, again, is there any way this can be done without using JS or other prog. languages?

    Cheers, folks!

    Ty

  2. #2
    Join Date
    Apr 2010
    Posts
    9
    Wow!
    Slow down, boys, slow down! One at a time, don't push! There is room for every one, no need to push in order to jump and help. Slow down!

    Ty

  3. #3
    Join Date
    Aug 2006
    Posts
    1,897
    I'm not sure your xml syntax on the name one was good, so I changed it

    test.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="test.xsl"?>
    
    <club_members>
    <member>
    <name>John Smith</name>
    <email>john.smith@mail.com</email>
    </member>
    <member>
    <name>Joe Smith</name>
    <email>joe.smith@mail.com</email>
    </member>
    <member>
    <name>Bob Smith</name>
    <email>bob.smith@mail.com</email>
    </member>
    <member>
    <name>Black Smith</name>
    <email>black.smith@mail.com</email>
    </member>
    <member>
    <name>Lock Smith</name>
    <email>lock.smith@mail.com</email>
    </member>
    </club_members>
    test.xsl:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
    <html>
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
    </head>
    <body>
    <p><xsl:for-each select="club_members/member">
    	<xsl:value-of select="name"/>
    	/
    	<xsl:value-of select="email"/>
    	<br />
    </xsl:for-each>
    </p>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    Save these off, and view test.xml

    Dave

  4. #4
    Join Date
    Apr 2010
    Posts
    9
    Thanks a lot, Dave, for your reply!
    Some time ago I received an assignment that stated the code structure should not be modified but to work and find a solution to display on the values from the code as is. And the code did look something like both the one displayed on both e.g.1 and e.g.2 in my message. It confused de heck out of me. I spent some time on some of the classic tutorials on the net (things like w3schools, tizag, etc) but I just can not find a solution. I did write something like the following which displayed ok in Internet Explorer but not in Firefox:

    1.
    <xsl:for-each select="book/title">
    <ul>
    <li><xsl:value-of select="list/child::node()"/></li>

    <li><xsl:value-of select="list/child::node()/following-sibling::node()"/></li>

    <li><xsl:value-of select="list/child::node()/following-sibling::node()
    /following-sibling::node()"/></li>
    <li><xsl:value-of select="list/child::node()/following-sibling::node()
    /following-sibling::node()
    /following-sibling::node()"/></li> </ul>

    ............

    And for the second:


    <table>
    <tr>
    <th>Club Member</th>
    <th>E-mail</th>
    </tr>
    <xsl:for-each select="club_members/member">
    <tr>
    <td><xsl:value-of select="node()"/></td>
    <td><xsl:value-of select="following-sibling::node()"/></td>
    </tr>
    </xsl:for-each>
    </table>

    .............

    But these solutions really look odd to me.

    Thanks again for takin time to help!!

    Ty

  5. #5
    Join Date
    Aug 2006
    Posts
    1,897
    They look odd to me too. But I should say that I tried to appreciate XSLT, but realistically I pretty much hate it. I find it far easier to parse and display XML with PHP's SimpleXML interface, and that's what I do*.

    Dave

    * as a hobby developer. I am not doing this for a living where it might somehow actually matter.

  6. #6
    Join Date
    Apr 2010
    Posts
    9
    Cheers, Dave. I'll keep on playing with it maybe I bump into something that will do it. I really wish I could use some third party solution to this 'coz I'm getting tired of it now.

    Ty

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles