I'm trying again to seek your help. This problem has been irking me for
a couple of days now and I'm at my wit's end trying to solve it. I have
significantly simplified my input and lookup files, so I'll appreciate
it greatly if somebody can take a look and help me.
I have the following input file, where the pref element indicates a
lookup field from a different file.
<?xml version="1.0" encoding="utf-8" ?>
<TM>
<T id="id1">
<pref>1</pref>
<T id="id2">
<pref>1</pref>
<T id="id3">
<pref>3</pref>
</T>
</T>
</T>
</TM>
The lookup file is as follows.
<?xml version="1.0" ?>
<AB>
<pa prefnum="1">
<XXXX>
<YYYY />
<ZZZZ />
</XXXX>
</pa>
<pa prefnum="2">
<WWWW />
</pa>
<pa prefnum="3">
<AAAA>
<BBBB />
<CCCC />
<DDDD>
<EEEE />
<FFFF />
</DDDD>
</AAAA>
</pa>
</AB>
Each T element in the original file has to be replaced by a set of p
elements whose attributes are set based on the lookup performed and the
id from the original file.
So the first T element with id = id1 and pref = 1 should be replaced by
the following
<p c="XXXX" id="id11">
<p c="YYYY" id="id112" />
<p c="ZZZZ" id="id114" />
</p>
I am able to do the lookup successfully, but am unable to maintain the
hierarchy of the source tree. This is my stylesheet
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="/">
<Root>
<xsl:apply-templates />
</Root>
</xsl:template>
<xsl:template match="//T">
<xsl:apply-templates
select="document('lookup.xml')//AB/pa[(_at_)prefnum=current()/pref]/*" mode="p">
<xsl:with-param name="id" select="@id" />
</xsl:apply-templates>
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="pref"></xsl:template>
<xsl:template mode="p" match="*">
<xsl:param name="id">IDENTIFIER</xsl:param>
<xsl:variable name="idd" select="translate($id,'
','_')"></xsl:variable>
<xsl:variable name="newid" select="concat($idd,
position())"></xsl:variable>
<p c="{name()}" id="{$newid}">
<xsl:apply-templates mode="p">
<xsl:with-param name="id" select="$newid" />
</xsl:apply-templates>
</p>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
Based on this, I'm getting the following output.
<?xml version="1.0" encoding="UTF-8" ?>
<Root>
<p c="XXXX" id="id11">
<p c="YYYY" id="id112" />
<p c="ZZZZ" id="id114" />
</p>
<p c="XXXX" id="id21">
<p c="YYYY" id="id212" />
<p c="ZZZZ" id="id214" />
</p>
<p c="AAAA" id="id31">
<p c="BBBB" id="id312" />
<p c="CCCC" id="id314" />
<p c="DDDD" id="id316">
<p c="EEEE" id="id3162" />
<p c="FFFF" id="id3164" />
</p>
</p>
</Root>
The correct output that I desire is
<?xml version="1.0" encoding="UTF-8" ?>
<Root>
<p c="XXXX" id="id11">
<p c="YYYY" id="id112" />
<p c="ZZZZ" id="id114" />
<p c="XXXX" id="id21">
<p c="YYYY" id="id212" />
<p c="ZZZZ" id="id214" />
<p c="AAAA" id="id31">
<p c="BBBB" id="id312" />
<p c="CCCC" id="id314" />
<p c="DDDD" id="id316">
<p c="EEEE" id="id3162" />
<p c="FFFF" id="id3164" />
</p>
</p>
</p>
</p>
</Root>
If I move the <xsl:apply-templates select="*" /> command from the T
template and move it within the mode=p template, then it works for only
the first T element. I'm assuming this is because the context has
switched to the lookup file and at that point, it's unable to find any
more T elements there. So how do I restore the context back to the
original file *at that point*?
Please help!
Thanks,
-Farooq.
--
Mir Farooq Ali
Computer Science, Virginia Tech,
Email: miali(_at_)cs(_dot_)vt(_dot_)edu
Web: http://purl.org/net/farooq
Office: 525 McBryde Hall
Tel: (540) 231-1927 (Office)
--------------------------------------------
(c) 2004 Mir Farooq Ali All rights reserved
--------------------------------------------