Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XPath using an element value in XSL

From: David Carlisle <davidc@---.--.-->
To: NULL
Date: 12/2/2004 4:53:00 PM
Pat Turner <purpletrousers@n...> writes:

> Thanks David,
> 
> errrm, could you give me an example of what the first pass stylesheet 
> would look like using my example? I can't think how it would be done.
> 
> TIA
> Pat

several ways, depending how generic/efficient you want to be.

for example

input doc (eval.xml):

<family>
	<person name="bob">
		<father ref="../../person[2]" />
	</person>
	<person name="charlie">
		<child ref="../../person[1]" />
	</person>
</family>


proto-stylesheet (eval1.xsl)


<xsl:stylesheet	version="1.0"
xmlns:x="data:,x"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template mode="a" match="person">
  <xsl:value-of select="@name"/>
</xsl:template>

<xsl:template match="person">
Person:  <xsl:value-of select="@name"/>
         <xsl:apply-templates select="father|child"/>
</xsl:template>

<xsl:template x:match="father">
Father: <xsl:value-of select="@name"/>
         <xsl:apply-templates mode="a" x:select="@ref"/>
</xsl:template>

<xsl:template x:match="child">
Child: <xsl:value-of select="@name"/>
         <xsl:apply-templates mode="a" x:select="@ref"/>
</xsl:template>

</xsl:stylesheet>


in the above the syntax (which I just made up) is that templates
depending on a generated xpath use x:match in their match pattern, and
use x:select where they want the xpath to appear. This could be made
more efficient (i generate all templates for all dynamic xpaths which is
less code for me to write but generates more templates than needed)



Evaluation stylesheet (this has original source doc filename hardcoded,
it could be a parameter)

<xsl:stylesheet	version="1.0"
xmlns:x="data:,x"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template mode="a" match="person">
  <xsl:value-of select="@name"/>
</xsl:template>

<xsl:template match="person">
Person:  <xsl:value-of select="@name"/>
         <xsl:apply-templates select="father|child"/>
</xsl:template>

<xsl:template x:match="father">
Father: <xsl:value-of select="@name"/>
         <xsl:apply-templates mode="a" x:select="@ref"/>
</xsl:template>

<xsl:template x:match="child">
Child: <xsl:value-of select="@name"/>
         <xsl:apply-templates mode="a" x:select="@ref"/>
</xsl:template>

</xsl:stylesheet>







generate real stylesheet
saxon -o eval2.xsl eval1.xsl eval.xsl

eval2.xsl has several templates expanded out. and looks like

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="data:,x" version="1.0">


<xsl:template mode="a" match="person">
  <xsl:value-of select="@name"/>
</xsl:template>

<xsl:template match="person">
Person:  <xsl:value-of select="@name"/>
         <xsl:apply-templates select="father|child"/>
</xsl:template>


<xsl:template match="father[@ref='../../person[2]']">
Father: <xsl:value-of select="@name"/>
         <xsl:apply-templates mode="a" select="../../person[2]"/>
</xsl:template>
<xsl:template match="father[@ref='../../person[1]']">
Father: <xsl:value-of select="@name"/>
         <xsl:apply-templates mode="a" select="../../person[1]"/>
</xsl:template>


<xsl:template match="child[@ref='../../person[2]']">
Child: <xsl:value-of select="@name"/>
         <xsl:apply-templates mode="a" select="../../person[2]"/>
</xsl:template>
<xsl:template match="child[@ref='../../person[1]']">
Child: <xsl:value-of select="@name"/>
         <xsl:apply-templates mode="a" select="../../person[1]"/>
</xsl:template>

</xsl:stylesheet>



run this stylesheet on original source:

$ saxon eval.xml eval2.xsl
<?xml version="1.0" encoding="utf-8"?>

Person:  bob
Father: charlie

Person:  charlie
Child: bob







transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent