XPATH construction from an attributes value

Here should go questions about transforming XML with XSLT and FOP.
pjl
Posts: 12
Joined: Tue May 16, 2006 1:16 pm

XPATH construction from an attributes value

Post by pjl »

pjl

I should start with I'm new to XSLT/XPATH.
I'm trying to use an attribute value in an input XML file as an XPATH expression to query whether an element with a named attribute exists in a second XML file. Basically I have the following:

Input XML file:

<Feature expression="/root/feature/option[name="dog"]"/>

Referenced XML file:

<root><feature><option name="cat"/></feature></root>

In the xsl file I've tried various things such as the following to look up whether the the expression evaluates to a location/element in the reference file:

<xsl:if test="document('input.xml')@expression"/>
which is invalid
or
<xsl:if test="document('input.xml', @expression)">
whcih returns true.

Any ideas as to how construct the XPATH expression or otherwise so as to be able to achieve this test?

Many thanks. Peter.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hello Peter,

You cannot use an XPath expression that you have as value directly in an XSLT. The general solution is to either perform this in two steps or you use some specific processor extension functions.

For instance you can have an XSLT stylesheet that applied on your forst document creates another XSLT stylesheet that (this second stylesheet) when applied on the second XML gives you the desired result. This is the best approach IMO as it is portable.

If you want the second approach the look into your processor documentation, for instance for Saxoon 6.5.5 the evaluate funtion is documented here:
http://saxon.sourceforge.net/saxon6.5.5 ... l#evaluate

Best Regards,
George
pjl
Posts: 12
Joined: Tue May 16, 2006 1:16 pm

Clarity

Post by pjl »

Thanks for the information. Also I wasn't entirely clear in my original posting and made a couple of mistakes:

I have 'Feature' elements in the input file (first.xml) which have attribute values which corresponds to an XPATH expression

e.g.

<...>

<Feature expression="/root/feature/option[@name='dog']"/>

<Feature expression="/root/feature/option[@name='cat']"/>

</...>

A second XML file (second.xml) has only one option element, e.g.:



<root><feature><option name="dog"/></feature></root>



During transforming of the first.xml file I want to look up to see which of the 'dog' or 'cat' options are selected in the second.xml file and generate differences in the output file accordingly.



so....during processing of first.xml I want to create a test based on a query constructed from the attribute value:



<xsl:apply-templates match="Feature[@expression]">

<xsl:if test="document('second.xml')//*****I want to use the value of @expression to form the look up***">

<Output>TRUE</Output>

</xsl:if>

</xsl:apply-templates>



Hence I'm expecting it to output TRUE when processing the first 'dog' feature and FALSE for the second 'cat' feature. The first question is how to use the expression attributes value in the look up and then how to correctly specify the boolean expression.
pjl
Posts: 12
Joined: Tue May 16, 2006 1:16 pm

Suggestions as to a better way to go?

Post by pjl »

I should also add that the use of an XPATH type expression within an attribute to perform the lookup was my intial solution to the problem. I basically need to hard-code some lookups within the input xml file to entries in a second xml file so any suggestions as the way to go would be appreciated.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

A follow-up is here
http://www.oxygenxml.com/forum/ftopic1776.html

Best Regards,
George
Post Reply