I need to pull XPath from strings obtained from the XML data source. A typical query might look like: XML: <library> <books> <book title="A" type="science fiction"/> <book title="B" type="fantasy"/> <book title="C" type="romance"/> <book title="D" type="science fiction"/> <book title="E" type="Christianity"/> </books> <librarian> <defaultXPath>//book[type="Christianity"]</defaultXPath> </librarian> </library> XSLT (works): <xsl:variable name="Books"> <xsl:apply-templates select="//book[type="Christianity"]"/> </xsl:variable> XSLT (fails): <xsl:apply-templates select="//librarian/defaultXPath"/> <xsl:apply-templates select="{//librarian/defaultXPath}"/> The problem is that the apply-templates command process the string as a data island rather than interpreting the string as an XPath query. I have tried using code from EXSLT but that has not worked and everything I read suggests that XSLT 2.0 might have a method to perform this operation, but I find no examples. Any suggestions?
XSLT 2.0 has no facility to do this either. Some processors have extension functions built in that do this, Saxon for instance has saxon:evaluate(). If you are using MSXML you can write an extension function in script, if using XML.NET then you can use a .NET language. It also might be useful to see what you tried in EXSLT that failed. -- Joe Fawcett (MVP - XML) http://joe.fawcett.name "Mycroft" wrote in message news:C00EE749-5633-46D5-A51F-64351B23028A@microsoft.com... >I need to pull XPath from strings obtained from the XML data source. A > typical query might look like: > > XML: > <library> > <books> > <book title="A" type="science fiction"/> > <book title="B" type="fantasy"/> > <book title="C" type="romance"/> > <book title="D" type="science fiction"/> > <book title="E" type="Christianity"/> > </books> > <librarian> > <defaultXPath>//book[type="Christianity"]</defaultXPath> > </librarian> > </library> > > XSLT (works): > <xsl:variable name="Books"> > <xsl:apply-templates select="//book[type="Christianity"]"/> > </xsl:variable> > > XSLT (fails): > <xsl:apply-templates select="//librarian/defaultXPath"/> > <xsl:apply-templates select="{//librarian/defaultXPath}"/> > > > The problem is that the apply-templates command process the string as a > data > island rather than interpreting the string as an XPath query. > > I have tried using code from EXSLT but that has not worked and everything > I > read suggests that XSLT 2.0 might have a method to perform this operation, > but I find no examples. > > Any suggestions? >
Mycroft wrote: > I have tried using code from EXSLT but that has not worked and everything I > read suggests that XSLT 2.0 might have a method to perform this operation, > but I find no examples. Saxon 9 is the current Saxon version that implements XSLT and XPath 2.0. It has http://www.saxonica.com/documentation/extensions/functions/evaluate.html and http://www.saxonica.com/documentation/extensions/functions/evaluate-node.html so with your sample XML corrected to (you had 'type' instead of '@type' which does not find anything) <library> <books> <book title="A" type="science fiction"/> <book title="B" type="fantasy"/> <book title="C" type="romance"/> <book title="D" type="science fiction"/> <book title="E" type="Christianity"/> </books> <librarian> <defaultXPath>//book[@type="Christianity"]</defaultXPath> </librarian> </library> and a stylesheet with <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" exclude-result-prefixes="saxon" version="2.0"> <xsl:template match="/"> <xsl:apply-templates select="saxon:evaluate-node(library/librarian/defaultXPath)"/> </xsl:template> <xsl:template match="book"> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet> the output is <?xml version="1.0" encoding="UTF-8"?><book title="E" type="Christianity"/> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
Joe, You asked me why EXSLT failed to work⦠I initially tried to use EXSLT which failed only because I stopped trying. The software I found for it was from 2001 and required a 30-day trial license key, which I did not have. So, while looking for other options I learned XSLT 2.0 was out and began researching it. I found comments about it that appeared to address this exact âXPath from a stringâ issue so I posted the question on this discussion board. I had been staying away from Saxon and EXSLT since they incur additional software license fees and issues I did not want to deal with (problems concerning software written for government use). Despite this, it looks as though I will have to use Saxon or EXSLT until I find a .NET alternative or can figure out how to code a custom solution. I am uncertain whether I am using XML.NET or MSXML. I am writing the XML and XSLT using VS 2005 and rendering it through IE7. So, I do not know which XML engine it is using. Sincerest thanks for your assistance.
Martin, Thank you for the code corrections! I wrote the example quickly without testing it, to provide an example of what I was trying to do. I now have a successful implementation using Saxon but using that incurs additional licenses and security/legal issues. Anyway, my code is working successfully using Saxon. It requires a different approach but it is functional. Do you know where I might find examples of linking the .NET XSLT engine to a custom function library? Perhaps I can code a small DLL to evaluate the variable just as Saxon does. Many thanks for your most timely assistance. Mycroft
Mycroft wrote: > Do you know where I might find examples of linking the .NET XSLT engine to a > custom function library? Perhaps I can code a small DLL to evaluate the > variable just as Saxon does. If you are talking about System.Xml.Xsl.XslCompiledTransform then you have two options, msxsl:script to write extension functions with C# or VB.NET or JScript.NET: http://msdn.microsoft.com/en-us/library/wxaw5z5e(VS.80).aspx or extension objects passed in with an XsltArgumentList: http://msdn.microsoft.com/en-us/library/tf741884(VS.80).aspx For an example see the post by Anton Lapounov in http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1809134&SiteID=1 Note however that your other post talks about running the transformation inside of IE 7, in that case the XSLT processor used is MSXML 3 and not the .NET processor XslCompiledTransform. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
Fantastic link references. Many thanks for your assistance!