XPATH construction from an attributes value
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 12
- Joined: Tue May 16, 2006 1:16 pm
XPATH construction from an attributes value
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.
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.
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
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
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
-
- Posts: 12
- Joined: Tue May 16, 2006 1:16 pm
Clarity
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.
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.
-
- Posts: 12
- Joined: Tue May 16, 2006 1:16 pm
Suggestions as to a better way to go?
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.
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service