creating xpath dynamically in xslt 2.0

Viewed 76

I want to fetch part of xml from a look-up-xml using some dynamic xpath located in my input xml. when I have tried to add the path hard-coded it is working perfect and getting the desired result for me. But when I am trying the same with a variable it is not working.

input

<root>
    <path
        location="/legitem/front[1]/title[1]">
        <msg>TITLE</msg>
    </path>
    <path
        location="/legitem/front[1]/longtitle[1]">
        <msg>LONGTITLE</msg>
    </path>
</root>

XSLT2.0

<xsl:variable name="fetch_content" select="document('source.xml')/legitem/front[1]/title[1]" as="node()"/>

<!-- working for above fixed xpath -->
<xsl:copy-of select="$fetch_content"/>

<!-- O\P : correct -->
--copies the content of <title>

But not fetching the content when trying to use variable to make dynamic

<xsl:variable name="LookUpXml" select="document('source.xml')" />
<xsl:variable name="loc" select="substring-after(@location, '/legitem/')"/>

<xsl:copy-of select="$main_file//$loc"/>

<!-- O\P : wrong -->
front[1]/title[1]
front[1]/longtitle[1]

If anyone has any idea on how I could achieve this that would be greatly appreciated! Thanks...

1 Answers

You can possibly do it by matching to the name() of the node, but it depends how much of the path is dynamic

<xsl:copy-of select="$main_file//front[1]/*[name()=$loc][1]" />
Related