Rod
Rod

Reputation: 15475

xsl take path and substring the value

I have this xsl path that gives me a desired value:

/path/to/@value

Is there a way to combine this into a substring?

substring(/path/to/@value, 1, 5)

The preceding statement does not work because I'm not as familiar to xsl as I thought

Upvotes: 0

Views: 185

Answers (1)

Kenney
Kenney

Reputation: 9093

Actually, it should work just fine:

XML:

<?xml version='1.0'?>

<path>
  <to value='123456'/>
</path>

XSLT:

<?xml version="1.0"?>

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:template match="/">
    <out>
      <xsl:value-of select='substring(/path/to/@value, 1, 5)'/>
    </out>
  </xsl:template>

</xsl:stylesheet>

Another way is to use an intermediate variable:

<xsl:variable name='t' select='/path/to/@value'/>
<xsl:value-of select='substring( $t, 1, 5 )'/>

Upvotes: 1

Related Questions