tags:

views:

29

answers:

2

I have two XSLT file A.xslt and B.xslt, where A.xslt includes B.xslt.

A.xslt

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:my="my_namespace">

    <my:data>This is A.xslt</my:data> 

    <xsl:include href="B.xslt"/>

    <xsl:template name="my_template">
        <xsl:value-of select="$remote-data"/>
    </xsl:template>

</xsl:stylesheet>

B.xslt

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:my="my_namespace">

    <xsl:variable name="remote-data"
                   select="document('')/*/my:data" />

</xsl:stylesheet>

Of course when my_template is evaluated, empty line is outputted. This happens because B.xslt does not cantain my:data container and document('') call is processed before include.

My question is: how can I access my:data from B.xslt (Make document('') to be processed after inclusion)?

Any ideas are appreciated!

Update: In real life I have multiple files like A.xslt: A1.xslt, A2.xslt,... and it is needed to make exactly the same calculations with nodeset as a result (like in $remote-data definition) but with different parameters (stored in my:data).

One can say that I can use named template for that. But as far as I can see it is not possible to do this, because template would return RTF, which I have to node-set() which is generally not available in XSLT 1.0 parsers.

Hope this clarifies my problem.

+1  A: 

The document() function invoked with an empty path will reference the source XML document of the XSLT that the code block "lives" in. In this case, it is B.xslt.

If you want to load/reference XML from A.xslt, then change your line to:

<xsl:variable name="remote-data"
                   select="document('A.xslt')/*/my:data" />
Mads Hansen
@Mads Hansen: +1 Good answer. The logic behind this `fn:document()` behavior is that an empty string argument is resolved as the **stylesheet URI** an the URI for `A.xslt` should be different from `B.xslt`.
Alejandro
Thank you for the answer, please see my update.
Savva Mikhalevski
@Savva: The update has nothing to do with the solution I am proposing -- it is sufficient to have the data defined in a single, global `<xsl:variable>` . Then this variable is accessible in all included/imported stylesheets. No need for any extension functions.
Dimitre Novatchev
A: 

There are two possibilities:

.1. As already recommended by @Mads-Hansen, use:

<xsl:variable name="remote-data" 
                   select="document('A.xslt')/*/my:data" /> 

.2. Move the definition of the $remote-data variable in A.xslt:

<xsl:variable name="remote-data"    
               select="document('')/*/my:data" />

Then it can be accessed by the code of B.xslt, because globally defined <xsl:variable> s are accessible in all included or imported stylesheet modules.

Dimitre Novatchev
Thank you for the answer, please see my update.
Savva Mikhalevski