Steve
Steve

Reputation: 55555

XPath Substring

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

Upvotes: 4

Views: 5157

Answers (3)

aepheus
aepheus

Reputation: 8187

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

substring-before(substring-after(NewsML/NewsItem/Identification/NameLabel,'-'),'-')

Upvotes: 8

Sean B. Durkin
Sean B. Durkin

Reputation: 12729

If you are using XSLT 2.0, then a better expression would be:

fn:replace( NewsItem/NewsML/Identification, "^.*-(.)-.*$", $1)

But if you are stuck with XSLT 1, then go with Aepheus's answer.

Upvotes: 1

mindandmedia
mindandmedia

Reputation: 6825

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

Upvotes: 1

Related Questions