开发者

Using XPath to retrieve substring after specific text

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

开发者_StackOverflow中文版
    <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?


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.


Use the string function substring-after, like so:

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

This'll return 1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜