Error; The context item is undefined

Questions about XML that are not covered by the other forums should go here.
henrikb
Posts: 2
Joined: Mon Oct 11, 2010 10:21 am

Error; The context item is undefined

Post by henrikb » Mon Oct 11, 2010 11:05 am

I created a function that analyzes an inputstring ($inputdata) and checks if a number of strings are in the text. It returns an an array of matching strings.
The strings to search are in a variable which is a document node with <root> a root node an <regex> as child nodes.

I want to create a function that passes this document node variable and a function that uses the variable to search the inputdata.

First I included the two functions (creating and processing of the document node variable) in one function. This works fine. (code 1)

Then I want to split this function into two functions and want create the document node as a variable in a separate function and return it to the main function. (code 2).
This gives the error: Axis step child::element{http://mysite/arrays}get_array, xs:anyType} cannot be used here: the context item is undefined
How can I define the context item?

Code 1 (all in one function: this works fine)
<xsl:function name="xsflist:find_cities_array" as="xs:string*" >
<xsl:param name="inputdata" as="xs:string"/>
<xsl:variable name="regex_prefix">
<root>
<regex>Paris</regex>
<regex>London</regex>
</root>
</xsl:variable>
<xsl:variable name="spec_regex_out" as="xs:string*">
<xsl:for-each select="$regex_prefix/root/regex" >
<xsl:variable name="str_regex" as="xs:string" select=".")" />
<xsl:analyze-string select="$inputdata" flags="i" regex="{$str_regex}" >
<xsl:matching-substring>
<xsl:sequence select="." />
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$spec_regex_out"/>
</xsl:function>

Code 2 now i want to split into two functions: but here I get an compile error)

<xsl:function name="xsflist:find_cities_in_data" as="xs:string*">
<xsl:param name="inputdata" as="xs:string"/>
<xsl:variable name="regex_list" >
<xsl:sequence select="xsfarrays:get_array" />
</xsl:variable>
<xsl:variable name="spec_regex_out" as="xs:string*">
<xsl:for-each select="$regex_prefix/root/regex" >
<xsl:variable name="str_regex" as="xs:string" select=".")" />
<xsl:analyze-string select="$inputdata" flags="i" regex="{$str_regex}" >
<xsl:matching-substring>
<xsl:sequence select="." />
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$spec_regex_out"/>
</xsl:function>

<xsl:function name="xsfarrays:get_cities_array" >
<xsl:variable name="regex_array" ">
<root>
<regex>Paris</regex>
<regex>London</regex>
</root>
</xsl:variable>
<xsl:sequence select="$regex_array" />
</xsl:function>

Your feedback is much appreciated!!

george
Site Admin
Posts: 2102
Joined: Thu Jan 09, 2003 2:58 pm

Re: Error; The context item is undefined

Post by george » Mon Oct 11, 2010 12:26 pm

Your samples have a few errors. However, after correcting those in the second sample I got it working without issues:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
exclude-result-prefixes="#all"
xmlns:xsflist="http://www.example.com/ns/xsflist"
xmlns:xsfarrays="http://www.example.com/ns/xsfarrays"
xmlns:xs="http://www.w3.org/2001/XMLSchema">


<xsl:template match="/">
<xsl:value-of select="xsflist:find_cities_in_data('Paris Test London')"/>
</xsl:template>

<xsl:function name="xsflist:find_cities_in_data" as="xs:string*">
<xsl:param name="inputdata" as="xs:string"/>
<xsl:variable name="regex_list" >
<xsl:sequence select="xsfarrays:get_cities_array()"/>
</xsl:variable>
<xsl:variable name="spec_regex_out" as="xs:string*">
<xsl:for-each select="$regex_list/root/regex">
<xsl:variable name="str_regex" as="xs:string" select="."/>
<xsl:analyze-string select="$inputdata" flags="i" regex="{$str_regex}" >
<xsl:matching-substring>
<xsl:sequence select="." />
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$spec_regex_out"/>
</xsl:function>

<xsl:function name="xsfarrays:get_cities_array">
<xsl:variable name="regex_array">
<root>
<regex>Paris</regex>
<regex>London</regex>
</root>
</xsl:variable>
<xsl:sequence select="$regex_array" />
</xsl:function>
</xsl:stylesheet>
Best Regards,
George
George Cristian Bina

henrikb
Posts: 2
Joined: Mon Oct 11, 2010 10:21 am

Re: Error; The context item is undefined

Post by henrikb » Mon Oct 11, 2010 1:14 pm

Hei George,

Thanks a mil for your quick reply. This helped me very much.

A number of mistakes occurred because I shortened the functions so that it was easier to read for you. BUT you found the error!!

When i call the seconday function <xsl:sequence select="xsfarrays:get_cities_array"/>

I forgot the (). It should be:

select="xsfarrays:get_cities_array()"/>

THANKS AGAIN FOR YOUR RAPID HELP!!

Post Reply