Re: Follow-up question [Fwd: [xsl] Question about variable-field lookup tables]

Subject: Re: Follow-up question [Fwd: [xsl] Question about variable-field lookup tables]
From: Mir Farooq Ali <miali@xxxxxxxxx>
Date: Wed, 21 Jan 2004 02:27:19 -0500
G. Ken Holman wrote:
At 2004-01-20 12:49 -0500, Mir Farooq Ali wrote:

How do I transmit the value of the id attribute in the XML file down to the newly created elements?


In your <xsl:apply-templates> you can use <xsl:with-param> to pass the id attribute value, and then in your <xsl:template> you use <xsl:param> to declare the expectation of receiving that value. You'll have to do this for all <xsl:apply-templates> instructions that will be triggered once engaged, which means you will have to override the built-in template rule for an element if that is one of the ones being used.

In the fragment below, the id is "something", so the XML that is generated should look something like this

      <p id="something1" c="XXXX">
         <p id="something11" c="YYYY"/>
         <p id="something12" c="ZZZZ"/>
      </p>

for the first case

and

<p id="something1" c="WWWW"/>

for the second case.


Once you have the id value, you can use numbering techniques for the suffix.

A broader question that I have is how to relate (or tie) the nodes in the main XML file when the context has switched to the lookup file.


By passing your desired value at *every* change in context, as I've described above.

I hope this helps.

........................ Ken

That was certainly helpful. Using a combination of concat and position(),
I was also able to get the numbering done. However, another issue needs
to be resolved.

Consider my XML file that looks like this
<?xml version="1.0" encoding="utf-8" ?>
<TM>
    <T id="something" TC="a">
		<Type>b</Type>
		<Prefs>
			<UC>S</UC>
			<UPS>1</UPS>
		</Prefs>
		<ST>
			<T id="somethinga" TC="a">
				<Type>b</Type>
				<Prefs>
					<UC>S</UC>
					<UPS>1</UPS>
				</Prefs>
				<ST>
					<T id="somethingb" TC="a">
						<Type>b</Type>
						<Prefs>
							<UC>S</UC>
							<UPS>1</UPS>
						</Prefs>
					</T>
					<T id="somethingc" TC="a">
						<Type>b</Type>
						<Prefs>
							<UC>S</UC>
							<UPS>2</UPS>
						</Prefs>
					</T>
				</ST>
			</T>
			<T id="somethingd" TC="a">
				<Type>b</Type>
				<Prefs>
					<UC>S</UC>
					<UPS>2</UPS>
				</Prefs>
			</T>
		</ST>
	</T>
    <T id="somethinge" TC="a">
		<Type>b</Type>
		<Prefs>
			<UC>S</UC>
			<UPS>2</UPS>
		</Prefs>
	</T>
</TM>

With the following lookup file
<AB>
   <TC name="a">
     <TTM type="b">
        <UCM preference="1">
           <UC>S</UC>
           <UPS>
              <pa prefnum="1">
                 <XXXX>
                   <YYYY/>
                   <ZZZZ/>
                 </XXXX>
              </pa>
              <pa prefnum="2">
                 <WWWW/>
              </pa>
           </UPS>
        </UCM>
     </TTM>
   </TC>
</AB>

The generated output should look something like this

<?xml version="1.0" encoding="utf-8" ?>
<S>
	<p id="something1" c="XXXX">
		<p id="something11" c="YYYY" />
		<p id="something12" c="ZZZZ" />
		
		<p id="somethinga1" c="XXXX">
			<p id="somethinga11" c="YYYY" />
			<p id="somethinga12" c="ZZZZ" />
			
			<p id="somethingb1" c="XXXX">
				<p id="somethingb11" c="YYYY" />
				<p id="somethingb12" c="ZZZZ" />
			</p>
			<p id="somethingc1" c="WWWW" />
		</p>
		<p id="somethingd1" c="WWWW" />
	</p>
	<p id="somethinge1" c="WWWW" />
</S>

What I am getting with a modified version of this spreadsheet that
Ken had kindly provided

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="T">
  <xsl:variable name="tc" select="@TC"/>
  <xsl:variable name="ttm" select="Type"/>
  <xsl:variable name="uc" select="Prefs/UC"/>
  <xsl:variable name="ups" select="Prefs/UPS"/>
  <xsl:for-each select="document('ali-lookup.xml')">
    <xsl:apply-templates mode="modify"
                         select="/AB/TC[@name=$tc]/TTM[@type=$ttm]/
                                 UCM[UC=$uc]/UPS/pa[@prefnum=$ups]/*"/>
  </xsl:for-each>
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="modify" priority="2">
  <p c="{name(.)}">
    <xsl:apply-templates mode="modify"/>
  </p>
</xsl:template>

<xsl:template match="@*|node()" mode="modify">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

is this

<?xml version="1.0" encoding="utf-8" ?>
<S>
	<p id="something1" c="XXXX">
		<p id="something11" c="YYYY" />
		<p id="something12" c="ZZZZ" />
	</p>
	<p id="somethinga1" c="XXXX">
		<p id="somethinga11" c="YYYY" />
		<p id="somethinga12" c="ZZZZ" />
	</p>
	<p id="somethingb1" c="XXXX">
		<p id="somethingb11" c="YYYY" />
		<p id="somethingb12" c="ZZZZ" />
	</p>
	<p id="somethingc1" c="WWWW" />
	<p id="somethingd1" c="WWWW" />
	<p id="somethinge1" c="WWWW" />
</S>

where every transformed node is at the same level. I want to preserve the hierarchy of the xml file. Any help is appreciated.

Thanks,

-Farooq.
--
Mir Farooq Ali

Computer Science, Virginia Tech,
Email: miali@xxxxxxxxx
Web: http://purl.org/net/farooq
--------------------------------------------
(c) 2004 Mir Farooq Ali  All rights reserved
--------------------------------------------


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



Current Thread