2

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comprehensive answer!
0

Use the string function substring-after, like so:

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

This'll return 1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.