XPath - Retrieves a numeric value from a string

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

I am new to xpath, xml and stylevision, so this could be a major issue.

I use 2010 styling and xpath to create sps / xslt for the circuit.

In the node above, you see that inside the node there is a numeric value, and I want to extract that value and turn it into a link in my pdf / html. The problem is that I cannot extract it. A substring is not an option, since the length of the value and the position of the numerical value inside it are different.

Some will probably think that the scheme is poorly structured and that the numerical value should be in a separate node / attribute / ... I can not do anything about it, since this scheme is provided by another company.

Thanks in advance!

+3
source share
5 answers

StyleVision 2010 seems to support XSLT 2.0, so you can use the stylesheet 2.0 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 line with the number is the context element inside the element <xsl:matching-substring>.

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

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

But if your input contains several numbers, it just combines them.

+8
source

Use this simple XPath 1.0 expression :

translate(.,translate(., '0123456789', ''), '')

Here is the complete XSLT 1.0 solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

when this conversion is applied to the provided XML document :

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

the desired, correct result is output:

822880494

Explanation

, . translate():

  • translate(). , .

  • translate(). , translate(). ().

+14

, XSLT 1.0 translate ( ) normalize-space ( , translate ). , . , translate , ascii.

XSLT 2.0 . xslt EXSLT, , .

p.s. , , .

+2

hi , ! , , .

XSLT 1

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    >
        <xsl:output method="xml" indent="yes"/>

        <xsl:template match="Root/Description">
            <xsl:call-template name="for-each-character">
                <xsl:with-param name="data" select="."/>
            </xsl:call-template>
        </xsl:template>

        <xsl:template name="for-each-character">
            <xsl:param name="data"/>
            <xsl:if test="string-length($data) &gt; 0">
                <xsl:if test="substring($data,1,1)&gt;-1">
                    <xsl:value-of select="substring($data,1,1)"/>
                </xsl:if>
                <xsl:call-template name="for-each-character">
                    <xsl:with-param name="data" select="substring($data,2)"/>
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
+2

XSLT v1, , , . . ( , "80 " "128,4 2", "2", "128,4", "128,42").

<xsl:template name="parseNumber">
<xsl:param name="data"/>
<xsl:param name="is-float" select="false()"/><!-- has this already been determined to be a non-integer -->
<xsl:if test="string-length($data) &gt; 0">
  <xsl:if test="(substring($data,1,1)&gt;-1) or ((substring($data,1,1) = '.') and (not($is-float)) )">
    <xsl:value-of select="substring($data,1,1)"/>
    <xsl:call-template name="parseNumber">
      <xsl:with-param name="data" select="substring($data,2)"/>
      <xsl:with-param name="is-float" select="(substring($data,1,1) = '.') or ($is-float)"/>
    </xsl:call-template>
  </xsl:if>
</xsl:if>
</xsl:template>

- :

Test: [123] ?=? numer(): [123] ?=? for-each-char: [123] ?=? parseNumber: [123]
Test: [1.23] ?=? numer(): [1.23] ?=? for-each-char: [1.23] ?=? parseNumber: [1.23]
Test: [1.1.1.1] ?=? numer(): [NaN] ?=? for-each-char: [1.1.1.1] ?=? parseNumber: [1.1]
Test: [123 abc] ?=? numer(): [NaN] ?=? for-each-char: [123] ?=? parseNumber: [123]
Test: [123 abc2] ?=? numer(): [NaN] ?=? for-each-char: [1232] ?=? parseNumber: [123]
Test: [123.456 abc7] ?=? numer(): [NaN] ?=? for-each-char: [123.4567] ?=? parseNumber: [123.456]
Test: [abc def ] ?=? numer(): [NaN] ?=? for-each-char: [] ?=? parseNumber: []
Test: [abc 123] ?=? numer(): [NaN] ?=? for-each-char: [123] ?=? parseNumber: []
0

Source: https://habr.com/ru/post/1662237/


All Articles