RE: [xsl] Navigating XML Using Attributes

Subject: RE: [xsl] Navigating XML Using Attributes
From: "Michael Semcheski" <mhs-list@xxxxxxxxx>
Date: Tue, 11 Feb 2003 17:50:17 -0500
This might help out if you want to get the xml into a more hierarchical
structure.  It ought to make the document a little more sensical (more
depth, less height).

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
	<xsl:template match="/*">
		<xsl:apply-templates select="*[position()=1]" />
	<!--<xsl:template match="*/child::*">-->
	<xsl:template match="*">
		<xsl:variable name="lname" select="name()"/>
		<xsl:element name="{$lname}">
			<xsl:attribute name="Id"><xsl:value-of
			<xsl:variable name="myID" select="@Id" />
			<xsl:if test="not(child::*)">
				<xsl:value-of select="."/>
select="*|following::*[@Id=$myID]" />

-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Pilarski,James
Sent: Tuesday, February 11, 2003 4:43 PM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Navigating XML Using Attributes

I have an XML document that was dumped from a database.  Instead of the
elements being nested in an appropriate, well-formed XML fashion, they are
associated with ID attributes.  The following XML is a generalized sample
with Account being the root element:

<?xml version="1.0" encoding="UTF-8"?>
			<LevelA3 Id="111"/>
			<LevelA3 Id="222"/>
			<LevelA3 Id="333"/>
	<LevelA3 Id="111">
			<LevelB2 Id="1"/>
			<LevelB2 Id="2"/>
			<LevelB2 Id="3"/>
			<LevelB2 Id="4"/>
	<LevelB2 Id="1">
	<LevelB2 Id="2">
	<LevelB2 Id="3">
	<LevelB2 Id="4">
	<LevelA3 Id="222">
			<LevelB2 Id="5"/>
			<LevelB2 Id="6"/>
			<LevelB2 Id="7"/>
			<LevelB2 Id="8"/>
	<LevelB2 Id="5">
	<LevelB2 Id="6">
	<LevelB2 Id="7">
	<LevelB2 Id="8">
	<LevelA3 Id="333">
			<LevelB2 Id="9"/>
			<LevelB2 Id="10"/>
			<LevelB2 Id="11"/>
			<LevelB2 Id="12"/>
	<LevelB2 Id="9">
	<LevelB2 Id="10">
	<LevelB2 Id="11">
	<LevelB2 Id="12">

To navigate the XML document, one must pull the Ids from element <LevelA3>
and return to the <Account> level to find the <LevelA3> elements that reside
in the document just below <Account>.  Once the appropriate <LevelA3>
element is found, the XSL must find its way to <LevelB2>, grab its Id,
return to the top and find <LevelB2> below.  This continues until the data
contained in <LevelC1> is found.  For example, the path to DataGamma is:
Account/LevelA1/LevelA2/LevelA3 Id="333"/LevelB1/LevelB2

I assume I will have to use apply-templates again and again.  What I don't
know is how to remember all the Ids in LevelA3, and below, so I can find
each of the cooresponding elements.  Any suggestions?

 XSL-List info and archive:

 XSL-List info and archive:

Current Thread