RE: [xsl] Generating an Xpath expression dynamically

Subject: RE: [xsl] Generating an Xpath expression dynamically
From: "Morgan, Corey" <CMorgan@xxxxxxxxx>
Date: Tue, 21 Oct 2003 10:50:36 -0600
Hello.

> I am trying to use XSLT to transform a set of XML files, each type with
> its own set of nodes, into a legacy file format. It is best if the XSLT
> was generic. I've tried to reference a second XML file that contains the
> transformation rules. This second XML file contains the relative XPath
> expression for each element in the source XML file and the target location
> in the legacy file format. I get the relative XPath string from the second
> XML file into a parameter, but when I try to use that parameter in a XSLT
> value-of element, it outputs the relative XPath string rather than the
> contents of that node in the source XML file.
> 
> Does anyone know how to output the contents of an element node in a source
> XML file using a XPath string read in from another XML file?
> Thanks.

An example may help.

If you're thinking of performing some kind of dynamic matches in your
stylesheet, you'll be disappointed. 
For example, the following will not work:

  <?xml version="1.0"?>
  <xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:variable name="dyn-xpath" select="'this/is[@nuts]'"/>
    <xsl:template match="$dyn-xpath">
       No good.
    </xsl:template>   
  </xsl:stylesheet>

You'll get the error: "Variables may not be used within this expression",
or some such.

Consider the following:

You have a "second XML file [that] contains the relative XPath expression 
for each element in the source XML file and the target location in the 
legacy file format".

But you know, that almost smells like XSL!  I don't know the origin of this
"second XML file", but you might consider converting that file into a 
stylesheet, and avoid the middleman.

If you can't/won't touch this second XML file, and/or you want to be a
bit trickier, why not _generate_ your XSL stylesheet from it.
After all, XSL _is_ XML, not merely to add verbosity to your life, but also 
so that it may be generated from some other stylesheet.

Hope this helps.

Corey Morgan

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


Current Thread