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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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
(Deleted User) Subject: Validate XPath on variable
Author: (Deleted User)
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
(Deleted User) Subject: Validate XPath on variable
Author: (Deleted User)
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
(Deleted User) Subject: Validate XPath on variable
Author: (Deleted User)
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
(Deleted User) Subject: Validate XPath on variable
Author: (Deleted User)
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

   
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.