Tempered
Tempered

Reputation: 35

Using XPath to retrieve substring after specific text

I have an xml document with multiple nodes that looks something like this:

    <Result>
        <Url>http://www.mysite.com/Topic/1/</Url>
    </Result>
    <Result>
        <Url>http://www.mysite.com/Topic/2/</Url>
    </Result>
    <Result>
        <Url>http://www.mysite.com/Topic/3/</Url>
    </Result>

What I want to do is to retrieve the Topic ID after "Topic/" for all nodes. For the above example I am looking for the values 1,2 and 3. I can think of other ways to do it but I was wondering if there was a way to do it using XPath?

Upvotes: 2

Views: 4839

Answers (2)

cordsen
cordsen

Reputation: 1701

You can use substring-after paired with substring-before to just extract the number

This is the expression

substring-before(substring-after(.,'http://www.mysite.com/Topic/'),'/')

Given your input (I added a root node to make it vaild)

<xml>
  <Result>
    <Url>http://www.mysite.com/Topic/1/</Url>
  </Result>
  <Result>
    <Url>http://www.mysite.com/Topic/2/</Url>
  </Result>
  <Result>
    <Url>http://www.mysite.com/Topic/3/</Url>
  </Result>
</xml>

this transform illustrates the result you want

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Url">
    <Topic>
      <xsl:value-of select="substring-before(substring-after(.,'http://www.mysite.com/Topic/'),'/')"/>
    </Topic>
  </xsl:template>
</xsl:stylesheet>

Output

<xml>
  <Result>
    <Topic>1</Topic>
  </Result>
  <Result>
    <Topic>2</Topic>
  </Result>
  <Result>
    <Topic>3</Topic>
  </Result>
</xml>

Depending on the context, you should replace . in the expression above with the path you need to access your node.

Upvotes: 3

Ian Wessman
Ian Wessman

Reputation: 51

Use the string function substring-after, like so:

substring-after(string(.),'http://www.mysite.com/Topic/')

This'll return 1/

Upvotes: 0

Related Questions