Subject: Apply a propertie file to a different xml file
Posted By: Clyne Post Date: 12/30/2006 10:39:39 AM
Hi folks,
I want to write a XSLT gets properties e.g. XPath-Expression from one file and apply it to a different one. I tried this:

<!--Propertie file:-->
<rule>
  <!-- pX is the old node; pX_ is a new node-->
  <xpath attribute1="p1" attribute2="p1_"/>
  <xpath attribute1="p2" attribute2="p2_"/>
</rule>


<!--XML file:-->
<test>
  <p1/>
  <p2/>
  <p3/>
</test>


<!--I want to have this output:-->
<test>
  <p1_/>
  <p2_/>
  <p3/>
</test>


<!--I tried this XSLT:-->
<xsl:param name="document" select="document('XMLfile.xml')"/>
<xsl:template match="/rule//xpath">
  <xsl:apply-templates select="$document" mode="document_">
    <xsl:with-param name="attribute1" select="./@attribute1" tunnel="yes"/>
    <xsl:with-param name="attribute2" select="./@attribute2" tunnel="yes"/>
  </xsl:apply-templates>
</xsl:template>
    
<xsl:template match="/" mode="document_">
  <xsl:param name="attribute1"/>
  xsl:apply-templates select="$attribute1" mode="tag_"/>
</xsl:template>

<xsl:template match="p1" mode="tag_">
  <xsl:param name="attribute2"/>
  <xsl:element name="{$attribute2}"/>
</xsl:template>

The code is not complete, but I failure to apply the p1-template. Could anybody help me?
Thanks a lot.
Clyne

Reply By: mhkay Reply Date: 12/30/2006 5:26:54 PM
There's no standard way in XSLT of constructing an XPath expression at run-time (or reading it from a file at run-time) and then evaluating it. Several products have extension functions to do this (e.g. saxon:evaluate() in Saxon). Alternatively, consider using XSLT to construct a stylesheet that incorporates the XPath expressions, and then executing that stylesheet.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: Clyne Reply Date: 12/30/2006 8:44:18 PM
Thanks Michael. I used the extension function but it doesn't work:

...
<xsl:apply-templates select="$document" mode="document_">
  <xsl:with-param name="attribute1" select="./@attribute1" tunnel="yes"/>
</xsl:apply-templates>
...
<xsl:template match="/" mode="document_">
  <xsl:param name="attribute1" select="default"/>
  <xsl:variable name="test" select="concat('/*//',$attribute1)"/>
  <xsl:value-of select="$attribute1"/>
  <xsl:apply-templates select="saxon:evaluate($test)" mode="tag_"/>
</xsl:template>

The value-of select gives the values of with-param back and overwrite default respectively. But in saxon:evaluate(..) I get an exception: Static error in XPath expression supplied to saxon:evaluate: XPath syntax error at char 4 in {/*//}: Unexpected token "<eof>" in path expression. Did I overlook a fault? Thanks at forward.

Reply By: mhkay Reply Date: 12/31/2006 10:17:37 AM
The argument supplied to saxon:evaluate() must be a valid XPath expression. "/*//" is not a valid XPath expression.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: mhkay Reply Date: 12/31/2006 10:19:18 AM
Looking more deeply, $attribute1 is taking its default value of "", because the supplied parameter said tunnel="yes" and the xsl:param declaration implies tunnel="no".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: Clyne Reply Date: 1/2/2007 9:56:03 AM
Thanks Michael. The point with the 'tunnel'-Attribute I did not see.


Go to topic 54250

Return to index page 77
Return to index page 76
Return to index page 75
Return to index page 74
Return to index page 73
Return to index page 72
Return to index page 71
Return to index page 70
Return to index page 69
Return to index page 68