Re: path quesion

Subject: Re: path quesion
From: Nick Browne <NickBrowne@xxxxxxxxxxxxxxx>
Date: Fri, 30 Jun 2000 12:00:09 +0100
Stephen, there were two possible interpretations of your request. I assume
you want the more complicated one, that is, where the element you want to
look up may change, i.e. at one point the value of $var may be 'customer/age'
and at another point it may be 'customer/sex'. Below is a solution with the
input files I used :

<?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="/">

  <xsl:for-each select="Root/Item">
   Item Key value :<xsl:value-of select="."/><BR />

   <!-- Store the cross reference data -->
   <xsl:variable name="CRData" select="document('twodocs01b.xml')"/>

   <!-- Get the Name value for the current Item -->
   <xsl:variable name="CRElement">Name</xsl:variable>
   Name :
   <xsl:value-of
select="$CRData/Root/Item[text()=current()]/*[name()=$CRElement]/text()"/><BR
/>

   <!-- Get the Desc value for the current Item -->
   <xsl:variable name="CRElement">Desc</xsl:variable>
   Desc :
   <xsl:value-of
select="$CRData/Root/Item[text()=current()]/*[name()=$CRElement]/text()"/><BR
/>
   <BR />
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

The main file contained :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<Root>
 <Item>1</Item>
 <Item>2</Item>
 <Item>3</Item>
 <Item>4</Item>
</Root>

... and the lookup file contained (twodocs01b.xml) :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<Root>
 <Item>1<Name>Name-1</Name>
              <Desc>Desc-1</Desc></Item>
 <Item>2<Name>Name-2</Name>
              <Desc>Desc-2</Desc></Item>
 <Item>3<Name>Name-3</Name>
              <Desc>Desc-3</Desc></Item>
</Root>

I did notice that the variable declaration must be done in the way shown,
i.e. don't use a select attribute. I'm afraid I have not checked the
subleties surrounding the reason for this. I imagine some optimisation could
be done, particularly if there are many lookup elements you want to extract
for the same key, perhaps by using another variable to hold just the subtree
of the required key and/or using xsl:key/key().

Inspiration for this came from the FAQ under Document#10.
--
Nick Browne
Slipstone Ltd



Stephen Kirkham wrote:

> I am processing 2 XML files within a single xsl file. >From one file
> I retreive a path value e.g. 'customer/age', from an element , this value
> is placed in a variable. I then want to look for the 'customer/age'
> element in the 2nd xml file, e.g.
>
>         variable $var contain the sting 'customer/age'
>
>      attempt to use this in a path expression for the second document
>
>     <xsl:value-of select="document('data.xml')/$var" />
>
> I get the following error
>
>    org.apache.xalan.xslt.XSLProcessorException: pattern =
> 'document('data.xml')/data/$id'
>
> thankful for any help
>
>  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