Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath Substring

Tags:

xml

xslt

xpath

I have an XML file that I need to transform using an XSL script.

Below is an example feed. I need to extract the text in the NameLabel element, but I only need the text specifically between the first two dashes.

For example, I want the A in the string below:

NTX-A-20120131-0006

I'm not well versed in XPath, so I'm struggling to put together an expression, however I'm assuming I need to use substring-(after|before). I'm just not sure how.

Sample XML:

<NewsML>
    <NewsItem>
        <Identification>
            <NameLabel>NTX-A-20120131-0006</NameLabel>
        </Identification>
    </NewsItem>
</NewsML>

Edit:

I am using xslt 1.0

like image 609
Steve Avatar asked Dec 28 '22 05:12

Steve


2 Answers

substring-before(s1,s2) and substring-after(s1,s2) would work.

substring-before(substring-after(NewsML/NewsItem/Identification/NameLabel,'-'),'-')
like image 108
aepheus Avatar answered Jan 01 '23 10:01

aepheus


try

<xsl:template match="/">
  <xsl:apply-templates select="NewsItem/NewsML/Identification"/>
</xsl:template>

<xsl:template match="Identification">
    <xsl:value-of select="substring-before(substring-after(NameLabel,'-'),'-'))"/>
</xsl:template>

but i couldn't test this

like image 25
mindandmedia Avatar answered Jan 01 '23 09:01

mindandmedia