Project

Profile

Help

saxon:evaluate() - Prefix xyz has not been d

Added by Anonymous about 15 years ago

Legacy ID: #7061389 Legacy Poster: Alexander Keim (alex116)

Hello, I'm a little bothered by the saxon:evaluate() function. The following XSLT instructions always result in the error message "Prefix xyz has not been declared". <xsl:variable name="expr" select="concat((if (starts-with(XPathExpr, '/')) then '' else '/') , XPathExpr)"/> <xsl:variable name="res" select="$srcDocument/saxon:evaluate($expr)"/> The mentioned namespace prefix is declared as part of $srcDocument and I thought when calling the function like this the context would shift to the variable and as such the namespace would be available. What I'm actually trying to do is to emulate the match-operation of a template-element. As such I want to check if the given XPath would find a node when used like <template match="XPathExpr"/> in a stylesheet. Any help is truely appreciated. Thanks! Regards - Alex


Replies (9)

Please register to reply

RE: saxon:evaluate() - Prefix xyz has not been d - Added by Anonymous about 15 years ago

Legacy ID: #7061560 Legacy Poster: Michael Kay (mhkay)

saxon:evaluate() takes its namespace context from the stylesheet. You may find that it's more appropriate to use saxon:evaluate-node(), which takes its namespace context from the document containing the XPath expression. That doesn't give you the chance to modify the XPath expression in the way you are doing. However, rather than modify the path expression, what you really seem to be doing is forcing it to be evaluated with the root as context node, which you could achieve by ensuring the context node is set appropriately, rather than by forcing a "/" at the start of the expression. Michael Kay http://www.saxonica.com/

RE: saxon:evaluate() - Prefix xyz has not been d - Added by Anonymous about 15 years ago

Legacy ID: #7061682 Legacy Poster: Alexander Keim (alex116)

Thanks for the fast reply! If I understand you correctly doing something like this <xsl:variable name="res" select="$srcDocument/saxon:evaluate-node(XPathExpr)"/> still won't help me, since it would result in a "Fatal Error! An empty sequence is not allowed as the first argument of saxon:evaluate-node()"-message. What is quite understandable since the required namespaces are declared within $srcDocument, which is different from the document where I extract the XPath from. Maybe you could point me into the right direction. I feel a little lost at the moment... Regards - Alex

RE: saxon:evaluate() - Prefix xyz has not been d - Added by Anonymous about 15 years ago

Legacy ID: #7062478 Legacy Poster: Michael Kay (mhkay)

Sorry, there are only two choices where to get the namespace context from: the stylesheet or the node containing the expression. There's no provision for creating an arbitrary namespace context or getting it from an unrelated node.

RE: saxon:evaluate() - Prefix xyz has not been d - Added by Anonymous about 15 years ago

Legacy ID: #7063585 Legacy Poster: Alexander Keim (alex116)

Sad to hear :o( What do you think about the following idea? <xsl:variable name="temp"> <xsl:value-of select="'&lt;Temp '"/> <xsl:value-of select="for $i in $srcDocument/child::[1]/namespace:: return concat('xmlns:', $i/local-name(), '=', '&quot;', $i, '&quot;')"></xsl:value-of> <xsl:value-of select="concat('&gt;', XPathExpr/text(), '&lt;/Temp&gt;')"></xsl:value-of> </xsl:variable> <xsl:variable name="expr"><xsl:copy-of select="saxon:parse($temp)"/></xsl:variable> <xsl:message>MATCH: <xsl:copy-of select="$srcDocument/saxon:evaluate-node($expr/Temp)"/></xsl:message> This way I'm adding the namespaces from the document that I want to perform the match to a variable, which I convert into a document using saxon:parse() and then apply the saxon:evaluate-node() function. Now there is no more error, but no node is found - even though a template with a similar match would find something...

RE: saxon:evaluate() - Prefix xyz has not been d - Added by Anonymous about 15 years ago

Legacy ID: #7063686 Legacy Poster: Michael Kay (mhkay)

Seems no reason why it shouldn't work, though it's rather convoluted. You don't actually need to construct the variable as lexical XML and then parse it, you can construct it as a tree: <xsl:variable name="expr" select="string(XPathExpr)"/> <xsl:variable name="temp"> <xsl:for-each select="$srcDocument/"> <xsl:copy> <xsl:value-of select="$expr"/> </xsl:copy> </xsl:for-each> </xsl:variable> ... then select="saxon:evaluate-node($temp/)" Or you could construct a query with namespace declarations in the Query Prolog, and then use saxon:query() instead of saxon:evaluate(). I can't comment on why your expression isn't finding anything - I don't have enough information to help you debug it.

RE: saxon:evaluate() - Prefix xyz has not been d - Added by Anonymous about 15 years ago

Legacy ID: #7064063 Legacy Poster: Alexander Keim (alex116)

Let me give you more details about my task at hand. I'm having an XML file with some kind of processing instructions. Here is a simplification of the file content <Files> <File> <Input>input.xml</Input> <Output>output_01.xml</Output> <Instructions> <Instruction> <XPathExpr>ns1:elem1/n2:elem2</XPathExpr> <Value>12345</Value> </Instruction> ... </Instructions> </File> ... </Files> My XSLT processes this file and applies the instructions to the respective input file to generate a modified output file. The file input.xml could be something like <ns1:root_elem xmlns:ns1="some/namespace/1" xmlns:ns2="some/namespace/2"> <ns1:elem1> <ns2:elem2>55</ns2:elem2> <ns2:elem3>green<ns2:elem3> </ns1:elem1> <ns1:elem4>big</ns1:elem4> </ns1:root_elem> For each of the instructions I'm creating a transformation, which is assigned to a variable and executed using saxon:transform() - that part works perfectly. Additionally I'd like to check if the given XPathExpr would actually match a node in $srcDocument. input.xml is loaded into $srcDocument and XPathExpr is, well, XPathExpr. When I gave your last suggestion a try I ended up with $temp being <ns1:root_elem xmlns:ns1="some/namespace/1" xmlns:ns2="some/namespace/2">ns1:elem1/n2:elem2</ns1:root_elem>, what I'm sure is not what you intended. Since you told me that the namespace may come from the document with the expression to evaluate I came up with idea of adding the namespaces to variable with the expression, convert it to a document and the process it with saxon:evaluate-node().

RE: saxon:evaluate() - Prefix xyz has not been d - Added by Anonymous about 15 years ago

Legacy ID: #7064090 Legacy Poster: Alexander Keim (alex116)

After sending my last post I understood what you meant and yes that's a simpler way of constructing the variable. Unfortunately my problem remains, that even with your proposal <xsl:value-of select="$srcDocument/saxon:evaluate-node($temp/*)"> never returns anything...

RE: saxon:evaluate() - Prefix xyz has not been d - Added by Anonymous almost 15 years ago

Legacy ID: #7434248 Legacy Poster: Alexander Keim (alex116)

After quite some time I found some spare minutes and came back to this problem. Now I'm creating and storing a stylesheet in a variable, run it through saxon:compile-stylesheet() and finally use saxon:transform() to evaluate the expression. Maybe my approach is somewhat complicated, but at least it's working after quite some attempts and disappointments with saxon:evaluate-node() and saxon:query()...

RE: saxon:evaluate() - Prefix xyz has not bee - Added by Anonymous almost 15 years ago

Legacy ID: #7436194 Legacy Poster: Andre (andre007)

.. we have changed with that problem to the Xpath API. Expression caching , dynamic namespaces and a userdefined FunctionContext are the advantages.. If you need such that all, change to java ;)

    (1-9/9)

    Please register to reply