Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

XML / XSL question

Options
  • 12-12-2006 12:12pm
    #1
    Posts: 0


    Hi all,

    Just going through some basic XSL tutorials and I'm curious as to how I would loop through all (if any) node elements .

    For example,

    <catalog>
    <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>1.90</price>
    <year>1985</year>
    </cd>

    <cd>
    <title>Apple Records</title>
    <artist>Funny Monkeys</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1995</year>
    </cd>
    </catalog>

    is my XML data and basically I'm looking for something like
    <xsl:for-each select="/cd/*">
    <xsl:value-of select="$1"/>
    </xsl:for-each>

    where $1 is the current element that is being processed.
    I would have thought this was a basic task but can't seem to find it anywhere on the web, I've messed around with different possibilities but with no luck.

    Any help would be appreciated on this, thanks (i'm sure it's a minor, niggly little issue somewhere)


Comments

  • Registered Users Posts: 2,931 ✭✭✭Ginger




  • Posts: 0 [Deleted User]


    Thanks but I've run through those tutorials, that specifies what elements are to be displayed, in the for-each loop.

    Basically, I want to loop through all elements in a node set (if any), without knowing what could appear. Ah, I'll just mess around more, hope to stumble on the solution.


  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    Couple of things.

    To filter for specific nodes, use Xpath. Xpath looks like a directory filter. Eg. to show all CD's for the year 1985 use
    <xsl:for-each select="//cd/[year='1985']">
    <xsl:value-of select="title" />
    </xsl:for-each>
    
    Do you want to actually display ALL the node info? i.e. in this case
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>1.90</price>
    <year>1985</year>
    
    ...

    hmmm, I think then you need to be stricter, and use either schemas or DTD's. The only way I can think of doing this is programming. In Java you could use something like JDOM to do this.
     SAXBuilder parser = new SAXBuilder();
     Document doc = parser.build(urlToXMLFile);
    // there are File implementations, I just cant be bothered checking what they are
    Iterator i = doc.getRootElement().getChildren("cd").iterator();
    while(i.hasNext()){
       Element e = (Element) i.next();
       out.println("<" + e.getName() + ">" + e.getText() + "</" + e.getName() + ">");
    }
    

    In the example above, you should obviously do some NodeType checking.

    HTH.


  • Posts: 0 [Deleted User]


    Hi, yeah that Java example is that type of operation I was thinking of, except done through the style sheets.

    Just thought there would have been an XPath expression to gather all of them and somehow, the stylesheet would hold the current element (whatever it may be) somewhere.

    If it can't be done through bare XSL, fair enough, just thought something could have been worked out.

    Thanks for your reply, I'm looking at the Java classes for XSL processing, look useful.


  • Registered Users Posts: 27,163 ✭✭✭✭GreeBo


    <xsl:template match="catalog">
    <xsl:for-each select=".">
    <xsl:value-of select="."/>
    </xsl:for-each>
    </xsl:template>

    be careful using "//" in xpath, its *very* inefficient as its scans through every node in the document.
    Use "/catalog/cd" instead.


  • Advertisement
Advertisement