Search code examples
xmlxsltxpathstylesheet

XPath - extract numeric value out of string


<Description>this is my value 822880494 this is my value</Description>

I'm quite new to xpath, xml and stylevision so this might be a basic problem.

I am using stylevision 2010 and xpath to create an sps/xslt for an schema.

In the above node you can see there is a numeric value inside the node and I want to extract that value and turn it into a link in my pdf/html. The problem is that i cant seem to extract it. Substring is not an option since the length of the value and the position of the numeric value inside it varies.

Some will probably think that the schema is badly composed and that the numeric value should be in a seperate node/attribute/... There is nothing I can do about that since this schema is provided by another company.

Thanks in advance!


Solution

  • StyleVision 2010 seems to support XSLT 2.0, so you could use a 2.0 stylesheet and do something like

    <xsl:analyze-string select='$foo' regex='\d+'>
      <xsl:matching-substring>
        <number><xsl:value-of select='.' /></number>
      </xsl:matching-substring>
    </xsl:analyze-string>
    

    Or whatever you want to do with the number; the string with the number is the context element inside the <xsl:matching-substring> element.

    Newtover's translate idea (for XSLT 1.0) would look like this:

    <xsl:value-of select="translate(., translate(., '0123456789', ''), '')" />
    

    But if your input contains multiple numbers, that will simply concatenate them.