Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

XSLT for XML?

I have the following xml

There are loads of items, hundreds of them.

Can I use xslt to "loop" and go through displaying all of them with the same bit of code?

The item id's change for each item.

<?xml version="1.0" encoding="UTF-8" ?>

<Item id=1>

<Name>name</Name>

<ScopeNotes>notes</ScopeNotes>

<HistoryNotes>history</HistoryNotes>

</Item>

<Item id=2>

<Name>name2</Name>

<ScopeNotes>notes2</ScopeNotes>

<HistoryNotes>history2</HistoryNotes>

</Item>

5 Answers

Relevance
  • BalRog
    Lv 5
    1 decade ago
    Favorite Answer

    First of all, the "XML" you have shown is not a document, but a document fragment. An XML document must have a unique root element that contains all other elements. This would not be too hard to fix; perhaps just add an "<Items>" start tag between lines 1 and 2, and an "</Items>" end tag after the last line.

    Moving on now...

    Yes you can use XSLT to "loop" through this input "document", or you can use templates to let XSLT's recursive event handler "loop" through it for you. Either way works, although the template approach results in a cleaner, more concise, more maintainable program.

    ---------------

    ASIDE TO nigel:

    The template-based approach is ONE way to use XSLT. To be sure it is generally the preferred approach. It takes full advantage of the language features and capabilities.

    However, the problem with the template-based approach is that it is extremely INPUT-driven. The template-based approach basically translated the input document into an output document. That means that the output document is constructed in fragmentary pieces and assembled as the templates are executed as dictated by the content of the input document.

    This can be less than ideal if non-XSLT-developers need to do design work on the output document, as is often the case when the output document is an HTML web page. In this case a document-based approach to XSLT usage may be better than a template-based approach.

    Examples to follow, time permitting...

  • 1 decade ago

    The way that XSLT works is by having a set of templates that are applied by certain conditions. So what you do is say that when you match an 'Item' element then process all the details of that item according to that template.

    This means you tend not to use loops - just rely on the same template getting triggered each time you find a particular element.

    www.w3schools.com has tutorials - but I'm sure there are others.

    Nige

  • 1 decade ago

    I would recommend using XPath to get all the items and then iterating through each item. For example

    XMLNodeList xml_itemNodes = xml_document.SelectNodes("\item");

    XMLNode xml_itemID = null;

    XMLNode xml_itemName = null;

    foreach(XMLNode xml_singleNode in xml_itemNodes)

    {

    xml_itemID = xml_singleNode.SelectSingleNode("@id");

    xml_itemName = xml_singleNode.SelectSingleNode("\Name");

    Console.WriteLine("ID: " + xml_itemID.InnerText.ToString());

    Console.WriteLine("Name: " + xml_itemName.InnerText.ToString());

    }

  • Anonymous
    1 decade ago
  • How do you think about the answers? You can sign in to vote the answer.
  • 5 years ago

    turn to google.

Still have questions? Get your answers by asking now.