1

I am working on a project where i have to send xpath to the application for process, some of the place i have to write repeated XPath. Is it possible to assign it into a variable and use that variable for repeatation:

EXAMPLE XML INPUT:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <element1>
        <element2>
            <element3>
                <element4>
                    <element5>1234567890</element5>
                </element4>
            </element3>
        </element2>
    </element1>
</root>

XPath i am using:

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

    <xsl:template match="root">
        <xsl:value-of select="
            if(string-length(/root/element1/element2/element3/element4/element5) > 6) 
            then (substring(/root/element1/element2/element3/element4/element5, string-length(/root/element1/element2/element3/element4/element5)-5)) 
            else /root/element1/element2/element3/element4/element5"/>
    </xsl:template>

</xsl:stylesheet>

NOTE:

  • This is not depend on XSLT, I just gave the example within XSLT code.
  • Here my need to extract last 6 digit of element5 if length greater than 6 otherwise print value of element5.
Amrendra Kumar
  • 1,806
  • 1
  • 7
  • 17
  • Are you aware that both XSLT https://www.w3.org/TR/xslt-30/#variables and XPath https://www.w3.org/TR/xpath-31/#id-let-expressions are publically documented W3C standards as well as explained in books like https://cranesoftwrights.github.io/books/ptux/index.htm? Searching in there for variables should help. – Martin Honnen Oct 05 '18 at 14:54

0 Answers0