XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Alex KeimSubject: Validate XPath on variable
Author: Alex Keim
Date: 04 Aug 2008 05:37 AM
Hi,

can anybody tell me if something like following is possible? I'm reading the content of an XML-file into a variable and would like to validate if an XPath expression which is defined in the context actually matches a node within the variable's temporary tree.

<xsl:template name="ValidateXPath">
<xsl:param name="root"/>
<xsl:for-each select="$root/msg:Entry">
<xsl:variable name="SrcFile" select="concat( '../xml/' , msg:InputFile)"/>
<xsl:variable name="SrcContent">
<xsl:copy-of select="document($SrcFile)/child::*[1]"/>
</xsl:variable>
<xsl:for-each select="msg:Values/msg:Value">
<xsl:if test="not($SrcContent[msg:XPathExpr])">
<xsl:message >No Match: </xsl:message>
<xsl:message select="msg:XPathExpr"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>

$srcContent contains the expected data und XPathExpr contains an XPath that, if applied to the document, would find a match. Nevertheless if combined as above it's just not working.

Any help is truly appreciated.

Cheers,
Alex

Postnext
Alberto MassariSubject: Validate XPath on variable
Author: Alberto Massari
Date: 04 Aug 2008 06:31 AM
Hi Alex,
XSLT does not allow to read text fragments from an XML document and treat them as literal XPath expressions; in order to do that, you need to use a non-standard extension function provided by your processor. Which processor are you using?

Alberto

Postnext
Alex KeimSubject: Validate XPath on variable
Author: Alex Keim
Date: 04 Aug 2008 07:00 AM
Hi Alberto,

I'm using Saxon9.

- Alex

Postnext
Alberto MassariSubject: Validate XPath on variable
Author: Alberto Massari
Date: 04 Aug 2008 07:49 AM
In this case, you can use saxon:parse (http://www.saxonica.com/documentation/extensions/functions/parse.html)

Alberto

Postnext
Alex KeimSubject: Validate XPath on variable
Author: Alex Keim
Date: 04 Aug 2008 08:26 AM
Thanks for pointing me to the Saxon homepage, but I just don't get the point.

- $SrcContent ist the XML stored in a variable
- XPathExpr is in the current scope/context and contains the XPath that I want to evaluate against $SrcContent

Do I have to use saxon:parse() on $SrcContent like in

<xsl:if test="not(saxon:parse($SrcContent)[msg:XPathExpr])"> ???

In this case I get an "org.xml.sax.SAXParseException: Content is not allowed in prolog" exception.

Postnext
Alberto MassariSubject: Validate XPath on variable
Author: Alberto Massari
Date: 04 Aug 2008 08:40 AM
Sorry Alex, I made some confusions when picking the correct extension function... my intention was to point you to saxon:evaluate (http://www.saxonica.com/documentation/extensions/functions/evaluate.html).
In order to assign the proper context you should do something like

<xsl:for-each select="$SrcContext">
<xsl:if test="saxon:evaluate(msg:XPathExpr)">
...

Alberto

Postnext
Alex KeimSubject: Validate XPath on variable
Author: Alex Keim
Date: 04 Aug 2008 09:08 AM
Originally Posted: 04 Aug 2008 09:01 AM
Ok, this makes much more sense to me, but I believe we are now having a kind of chicken-egg-problem...

This is how I started

<xsl:for-each select="msg:Values/msg:Value">
<xsl:if test="not($SrcContent[msg:XPathExpr])">
<xsl:message >No Match: </xsl:message>
<xsl:message select="msg:XPathExpr"/>
</xsl:if>
</xsl:for-each>

If I try what you suggest, I end up with the following

<xsl:for-each select="msg:Values/msg:Value">
<xsl:for-each select="$SrcContent">
<xsl:if test="saxon:evaluate(msg:XPathExpr)">
<xsl:message >No Match: </xsl:message>
<xsl:message select="msg:XPathExpr"/>
</xsl:if>
<xsl:for-each
<xsl:for-each

=> An empty sequence is not allowed as the first argument of saxon:evaluate()

I guess the saxon:evaluate() cannot access the msg:XPathExpr element, because it is not in the context of $srcContent...

UPDATE:

It works when I'm storing the XPath to evaluate in a variable...

<xsl:for-each select="msg:Values/msg:Value">
<xsl:variable name="localXpath" select="msg:XPathExpr"/>
<xsl:for-each select="$SrcContent">
<xsl:if test="saxon:evaluate($localXpath)">
<xsl:message >No Match: </xsl:message>
<xsl:message select="msg:XPathExpr"/>
</xsl:if>
<xsl:for-each
<xsl:for-each

... and now I'm having the next problem...

Static error in XPath expression supplied to saxon:evaluate: XPath syntax error at char 30 in {.../abc:Elem...}:
Prefix abc has not been declared

This is because the namespaces used in $srcContent are not known in my stylesheet and I don't want to declare them there.

Posttop
Alberto MassariSubject: Validate XPath on variable
Author: Alberto Massari
Date: 04 Aug 2008 09:12 AM
If your expression contains references to other namespaces, you will have to investigate which one of the other Saxon functions (eval(), expression() ) should be used to correctly prepare the context for parsing the XPath.

Alberto

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.