|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Flat XML: Problem getting children
Jacqui Moore wrote:
I have an xml file that has the following structure:Hi Jacqui, This might be simpler than it seems at first glance! It looks like a pretty flat XML file but with some indentation, it becomes clear that the information is already nicely grouped in <concept> elements. Which is good for you, because it means you don't need to do any grouping yourself. You want to use the <NT> elements to look up other <concept> elements, so you could start with a key on these elements by their descriptor: <xsl:key name="concepts" match="concept" use="descriptor"/> But some descriptor strings have trailing spaces, and <NT>Lounge</NT> will not match <descriptor> Lounge </descriptor>. That can be solved with normalize-space: <xsl:key name="concepts" match="concept" use="normalize-space(descriptor)"/> NT - the children categoriesOne of these is redundant, you can ignore the <BT> elements if you work from top to bottom: start with the first <concept> and recursively add nodes by their <NT> children. <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:key name="concepts" match="concept" use="normalize-space(descriptor)"/> <xsl:template match="/thesaurus"> <result> <xsl:apply-templates select="concept[1]"/> </result> </xsl:template> <xsl:template match="concept"> <concept name="{normalize-space(descriptor)}"> <xsl:apply-templates select="NT"/> </concept> </xsl:template> <xsl:template match="NT"> <xsl:apply-templates select="key('concepts', normalize-space(.))"/> </xsl:template> </xsl:stylesheet> You forgot the kitchen in the sample input, but after adding one, this is the output: <result>
<concept name="House">
<concept name="Rooms">
<concept name="Lounge"/>
<concept name="Kitchen">
<concept name="Utensils"/>
</concept>
</concept>
<concept name="Roofs">
<concept name="Kitchen">
<concept name="Utensils"/>
</concept>
</concept>
</concept>
</result>Cheers, Anton The objective is to create a expandable client side tree structure, and the user will click on a category and the category information (SN) will be displayed in the right frame.
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








