load external xml in xslt 1.0 with document() function but pass url of document as variable

12,361

I think your problem is the base of a relative URI. When the argument to document() is evaluated as a string, the base URI is the stylesheet. When the argument to document() is evaluated as a node, the base URI is the base URI of the node.

You've declared the variable in a way that it is getting used as a string by the document() function: as a result tree fragment.

If you declare the variable using select="some_node" instead of a child xsl:value-of then that changes the data type of the argument to the document() function, which changes the base URI of the evaluation, which may change the result.

Otherwise, your first line is correct: document($variable)

Share:
12,361
user2732475
Author by

user2732475

Updated on August 03, 2022

Comments

  • user2732475
    user2732475 almost 2 years

    Quick overview of problem: Iam transforming an xml with xslt 1.0, in that xml are nodes that have urls. With xsl 1.0 i can get those node values, when i get those values i need to use them to load another xml file from xslt, it works if i manually use those node values with document() function but i need to do it automatically.

    Loading external xml in xslt is quite straight forward with:

    <xsl:copy-of select="document('URL_OF_XML')/node1/node2"/> 
    

    and it works as it should.

    But if my url of external xml is stored in a variable,like:

    <xsl:variable name="MY_VARIABLE">
        <xsl:value-of select="some_node_containing_url"/>
    </xsl:variable> 
    
    <xsl:copy-of select="document('HOW_TO_USE_VARIBALE_VALUE_HERE') />
    

    I have tried:

    <xsl:copy-of select="document($MY_VARIABLE)" /> -> works after see UPDATE 2 below
    
    <xsl:copy-of select="document('$MY_VARIABLE')" /> nothing happend
    
    <xsl:copy-of select="document({$MY_VARIABLE})" /> nothing happend, ofc {} are for attributes
    
    <xsl:copy-of select="document('<xsl:value-of select="MY_VARIABLE"/>')" /> we cant use "<" ">" in document function
    

    Maybe I'm going the wrong path, and any kind of help would be appreciated, ty in advance

    UPDATE 1 -works: Got it work using concat and adding an empty string to my variable like:

    <xsl:copy-of select="document(concat('',$MY_VARIABLE))"/>
    

    UPDATE 2 -works: Following @G. Ken Holman suggestion i redecalared my variable from:

    <xsl:variable name="MY_VARIABLE">
            <xsl:value-of select="some_node_containing_url"/>
    </xsl:variable> 
    

    to:

    <xsl:variable name="MY_VARIABLE" select="some_node" />
    

    and just call my copy-of:

    <xsl:copy-of select="document($MY_VARIABLE)"/>
    

    and all works. Thanks for the help and suggestions! CONCLUSION: the only problem was my variable declaration, now the document() function accepts all calls to $MY_VARIABLE, no concat needed, but works also , and string($MY_VARIABLE) works after redeclaration. Thanks everyone for their time an help!