-1

I have this request coming in xml. Its a CDATA

<cmd>
     <![CDATA[HG<><><36.75><0420>< ><HS6011201700446279><><>< >< ><  ><><><  ><><  ><  ><><  ><><  ><>]]>
</cmd>

I need to extract HS6011201700446279 from the cdata path.

Following is the regex they gave. How to use this in xsl

HG<\\s*><\\s*><.*><.*><.*><[A-Z]{2}(\\d{10,}).*
mnvbrtn
  • 558
  • 1
  • 8
  • 27
  • Take a look at this http://stackoverflow.com/questions/8916208/how-do-i-use-a-regular-expression-in-xslt-1-0 – Shafizadeh Apr 22 '16 at 20:52
  • Why do you have literal `>`s in an xml file? If you really have these characters in an "xml file", your file isn't an xml file and you can't use xslt with it. Otherwise, perhaps you should edit your question and show what exactly contains your file. Actually, your question is totally unclear. – Casimir et Hippolyte Apr 22 '16 at 20:59
  • Actually its CDATA in xml. updated the input xml – mnvbrtn Apr 22 '16 at 21:51
  • Probably you need to use an extension (like scripts) - there is no regex built-in in xslt 1.0. You could also split the string and process it manually - it's a bit painful in pure xslt 1.0 though. – Pawel Apr 22 '16 at 23:14
  • Check if your xslt processor support the EXSLT regex library http://exslt.org/regexp/ – hr_117 Apr 23 '16 at 05:56

1 Answers1

2

There is no regex support in XSLT 1.0. Assuming that the sub-string you want is within the 6th "tag" of the given string, you could extract it by calling a recursive named template:

<xsl:template match="cmd">
    <result>
        <xsl:call-template name="get-Nth-value">
            <xsl:with-param name="list" select="."/>
            <xsl:with-param name="N" select="6"/>
        </xsl:call-template>
    </result>
</xsl:template>

<xsl:template name="get-Nth-value">
    <xsl:param name="list"/>
    <xsl:param name="N"/>
    <xsl:param name="delimiter" select="'>&lt;'"/>
    <xsl:choose>
        <xsl:when test="$N = 1">
            <xsl:value-of select="substring-before(concat($list, $delimiter), $delimiter)"/>
        </xsl:when>
        <xsl:when test="contains($list, $delimiter) and $N > 1">
            <!-- recursive call -->
            <xsl:call-template name="get-Nth-value">
                <xsl:with-param name="list" select="substring-after($list, $delimiter)"/>
                <xsl:with-param name="N" select="$N - 1"/>
                <xsl:with-param name="delimiter" select="$delimiter"/>
            </xsl:call-template>
        </xsl:when>
    </xsl:choose>
</xsl:template> 
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • can we use exslt regex? – mnvbrtn Apr 25 '16 at 17:01
  • If your processor supports it. Which processor will you be using? – michael.hor257k Apr 25 '16 at 17:58
  • we are using datapower. it supports exslt functions – mnvbrtn Apr 26 '16 at 23:26
  • Then I suppose you can. At least [the documentation](ftp://public.dhe.ibm.com/software/integration/datapower/library/prod_docs/4Q2008/3.7.2-ExtensionFunctionsCatalog.pdf) says you can. I am not a user myself, so I cannot speak from experience. -- Note that every XSLT 1.0 processor supports **some** EXSLT functions - that doesn't means it supports **all** of them. Regex support in particular is very rare. – michael.hor257k Apr 27 '16 at 09:11