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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:template match="/*">
		<xsl:apply-templates select="*[position()=1]" />
	</xsl:template>
	<!--<xsl:template match="*/child::*">-->
	<xsl:template match="*">
		<xsl:variable name="lname" select="name()"/>
		<xsl:element name="{$lname}">
			<xsl:attribute name="Id"><xsl:value-of
select="@Id"/></xsl:attribute>
			<xsl:variable name="myID" select="@Id" />
			<xsl:if test="not(child::*)">
				<xsl:value-of select="."/>
			</xsl:if>
			<xsl:apply-templates
select="*|following::*[@Id=$myID]" />
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>

-----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"?>
<Account>
	<LevelA1>
		<LevelA2>
			<LevelA3 Id="111"/>
			<LevelA3 Id="222"/>
			<LevelA3 Id="333"/>
		</LevelA2>
	</LevelA1>
	<LevelA3 Id="111">
		<LevelB1>
			<LevelB2 Id="1"/>
			<LevelB2 Id="2"/>
			<LevelB2 Id="3"/>
			<LevelB2 Id="4"/>
		</LevelB1>
	</LevelA3>
	<LevelB2 Id="1">
		<LevelC1>data</LevelC1>
	</LevelB2>
	<LevelB2 Id="2">
		<LevelC1>data</LevelC1>
	</LevelB2>
	<LevelB2 Id="3">
		<LevelC1>data</LevelC1>
	</LevelB2>
	<LevelB2 Id="4">
		<LevelC1>data</LevelC1>
	</LevelB2>
	<LevelA3 Id="222">
		<LevelB1>
			<LevelB2 Id="5"/>
			<LevelB2 Id="6"/>
			<LevelB2 Id="7"/>
			<LevelB2 Id="8"/>
		</LevelB1>
	</LevelA3>
	<LevelB2 Id="5">
		<LevelC1>data</LevelC1>
	</LevelB2>
	<LevelB2 Id="6">
		<LevelC1>data</LevelC1>
	</LevelB2>
	<LevelB2 Id="7">
		<LevelC1>data</LevelC1>
	</LevelB2>
	<LevelB2 Id="8">
		<LevelC1>data</LevelC1>
	</LevelB2>
	<LevelA3 Id="333">
		<LevelB1>
			<LevelB2 Id="9"/>
			<LevelB2 Id="10"/>
			<LevelB2 Id="11"/>
			<LevelB2 Id="12"/>
		</LevelB1>
	</LevelA3>
	<LevelB2 Id="9">
		<LevelC1>DataAlpha</LevelC1>
	</LevelB2>
	<LevelB2 Id="10">
		<LevelC1>DataBeta</LevelC1>
	</LevelB2>
	<LevelB2 Id="11">
		<LevelC1>DataGamma</LevelC1>
	</LevelB2>
	<LevelB2 Id="12">
		<LevelC1>DataDelta</LevelC1>
	</LevelB2>
</Account>

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
Id="11"/LevelC1:DataGamma

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:  http://www.mulberrytech.com/xsl/xsl-list



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread