RE: [xsl] Searching in Sub-children from an Array

Subject: RE: [xsl] Searching in Sub-children from an Array
From: "Houman Khorasani" <Houman.Khorasani@xxxxxxxxxxx>
Date: Thu, 7 Dec 2006 12:14:54 -0000
David,


I have a bit of a problem making this solution work properly: I have two
questions:

1) According to your suggestion, I have tried the following XSL

I have made a demo XML file for a better understanding of the problem:

<tree>
	<fruit ID="111">
		<mango ID="333">
			<color>Yellow</color>
		</mango>
		<orange ID="222">
			<color>orange</color>
		</orange>
	</fruit>
	<fruit_adjustment>
		<color>green</color>
		<Steps>111,333</Steps>
	</fruit_adjustment>
</tree>


The idea is to check <Steps> and see which fruit needs a new color. In
this example it is the mango; Its color should be adjusted to Green.
The expected output should be

<tree>
	<fruit ID="111">
		<mango ID="333">
			<color>green</color>
		</mango>
		<orange ID="222">
			<color>orange</color>
		</orange>
	</fruit>
</tree>

How do I have to proceed? I have tried this XSL below just to get an
idea, however instead I get this output:
<?xml version="1.0" encoding="utf-8"?>Yelloworangegreen

Which doesn't give me any clue...


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.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="ids" match="*" use="@ID"/>

	<xsl:template match="Steps">
		<xsl:variable name="ids" select="tokenize(.,',\s*')"/>
		<xsl:apply-templates mode="step"
select="key('ids',$ids[1])[1]">
			<xsl:with-param name="ids"
select="$ids[position()!=1]"/>
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template mode="step" match="*">
		<xsl:param name="ids"/>
		<xsl:apply-templates mode="step"
select="key('ids',$ids[1],.)[1]">
			<xsl:with-param name="ids"
select="$ids[position()!=1]"/>
		</xsl:apply-templates>
	</xsl:template>
</xsl:stylesheet>



2) I don't know if the thing above is possible at all, but even if so,
XML Mapping seems to be a bit slow, so I though as an alternative
solution, I could produce the <Steps> with a proper XPATH like this:

<tree>
	<fruit ID="111">
		<mango ID="333">
			<color>Yellow</color>
		</mango>
		<orange ID="222">
			<color>orange</color>
		</orange>
	</fruit>
	<fruit_adjustment>
		<color>green</color>
		<Steps>//*[@ID='111']//*[@ID='333']</Steps>
	</fruit_adjustment>
</tree>

Would I be then able to copy the <fruit> element and all its children
over and find only the fruit that needs a color change (according to the
XPath in <Steps>) and apply the color change?


Many Thanks,
Houman

Current Thread