Jump to content

Maintaining the xml order


yarivt

Recommended Posts

Hi,I have this xml (generated by a server):

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="test.xsl"?><Lists>	<PeopleList order="1">		<item>			<Name>Adult A</Name>		</item>		<item>			<Name>Adult B</Name>		</item>	</PeopleList>	<KidList order="2">		<item>			<Name>Kid A</Name>		</item>		<item>			<Name>Kid B</Name>		</item>	</KidList></Lists>

And this is the xsl template used by the xml:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">	<html>	<body>		<xsl:apply-templates select="Lists"/> 	</body>	</html></xsl:template><xsl:template match="Lists">	<h2>My List</h2>	<xsl:apply-templates select="KidList"/> 	<xsl:apply-templates select="PeopleList"/> </xsl:template><xsl:template match="PeopleList">	<p> People List </p></xsl:template><xsl:template match="KidList">	<p> KidList </p></xsl:template></xsl:stylesheet>

The results are:My ListKidList People List As you can see, it is the "xsl:apply-templates" commands that defined the order of the result (in the xml KidList was after PeopleList).The order in the xml is matter and I want one xsl that displays the xml in the right order.Any suggestions?Thanks, Yariv

Link to comment
Share on other sites

Are all lists named *List? If so, then try this:

<xsl:template match="Lists">	<h2>My List</h2>	<xsl:apply-templates select="*"/></xsl:template><xsl:template match="*[contains(local-name(), 'List')]">	<p> <xsl:value-of select="substring-before(local-name(), 'List')"/> List </p></xsl:template>

The template match matches all elements in document order. But the only template that gets executed is the next one, which only matches all elements that contain "List" in their name.

Link to comment
Share on other sites

Are all lists named *List? If so, then try this:
<xsl:template match="Lists">	<h2>My List</h2>	<xsl:apply-templates select="*"/></xsl:template><xsl:template match="*[contains(local-name(), 'List')]">	<p> <xsl:value-of select="substring-before(local-name(), 'List')"/> List </p></xsl:template>

The template match matches all elements in document order. But the only template that gets executed is the next one, which only matches all elements that contain "List" in their name.

Thank you very much for replying. You gave me a simple and better idea:
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body>	<xsl:apply-templates select="Lists"/> </body></html></xsl:template><xsl:template match="Lists">	<h2>My List</h2>	<xsl:for-each select="*">	<xsl:variable name="TemplateName" select="."/>		<xsl:apply-templates select="$TemplateName"/>	</xsl:for-each>	</xsl:template><xsl:template match="PeopleList">	<p> People List </p></xsl:template><xsl:template match="KidList">	<p> KidList </p></xsl:template>		</xsl:stylesheet>

Thank, Yariv

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...