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?

-------------Problems Reply------------

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/

Category:xml Views:0 Time:2011-06-16
Tags: xml xpath

Related post

  • Xpath python find node after specific text 2011-08-25

    Here is the HTML code: <div id="someid"> <h2>Specific text 1</h2> <a class="hyperlinks" href="link"> link1 inside specific text 1</a> <a class="hyperlinks" href="link"> link2 inside specific text 1</a> <a

  • Xpath: how do you select the second text node (specific text node) 2009-10-18

    consider a html page <html> apple orange drugs </html> how can you select orange using xpath ? /html/text()[2] doesn't work. --------------Solutions------------- You cant do it directly by selecting. You need to call an xpath string funct

  • Nokogiri Xpath to retrieve text after within and 2012-04-04

    I have the following html and like to know how to use xpath to retrieve all the info: - Name(first, last) - Nick Name - email - shipping address... Primarily, retrieve text after <BR>. Many Thanks in advance. <table> <tr> <td val

  • How do I retrieve the locale-specific date format string in Flex / ActionScript 3? 2010-02-01

    How do I retrieve the locale-specific date format string in Flex / ActionScript 3? I am unable to find a method to return the actual format string (that which specifies the date format) based on the current locale. I am asking this question because I

  • scrolling vertically to a specific text part in a webview 2011-01-20

    I'm looking for a way to scroll a webview that a specific part of the contained text is shown. Scrolling programmatically needs always an integer value of how far to scroll. Is there any possibility of retrieving the y-position of a specific text in

  • How to extract specific text from a webpage? 2011-09-18

    This question already has an answer here: Text Extraction from HTML Java 9 answers I'm trying to extract a specific text from a webpage? This is the part of the webpage which contains the specific text: <div class="module"> <div class="body"

  • Find specific text in multiple TXT files in PHP 2011-11-07

    I want to find a specific text string in one or more text files in a directory, but I don't know how. I have Googled quite a long time now and I haven't found anything. Therefor I'm asking you guys how I can fix this? Thanks in advance. -------------

  • Extracting numbers from a range of cells preceeded by specific text. 2012-04-02

    I am trying to separate up to two values from a range of cells containing text and numbers i.e. from a cell containing "FH4+V8", I would like to extract the 4 into one column and the 8 into another. Actually I would like to sum all the values preceed

  • How to search for a specific text and echo next characters of that specific text? 2012-09-29

    I have a project requirement where next characters of a text need to be printed after searching a specific text. To understand better, here is an example. I have an index.html file and would like to print a specific text after searching the text. Que

  • Change color of specific text only in text box of BlackBerry 2009-05-22

    How to change color of specific text only in textbox for Blackberry applications? --------------Solutions------------- As Fostah said in his comment, the TextBox class isn't very flexible in terms of changing the look and feel. Even the look and feel

  • Regex PHP, Match all links with specific text 2009-11-02

    I am looking for a regular expression in PHP which would match the anchor with a specific text on it. E.g I would like to get anchors with text mylink like: <a href="blabla" ... >mylink</a> So it should match all anchors but only if they

  • JQuery: Select elements that are before an anchor with a specific text 2009-11-08

    I have a div having a css class "MyClass". Inside this div, I can have any number of checkboxes immediately followed by an anchor having some specific text (lets say MyText for the example). How can I select all the checkbox elements using JQuery. Wh

  • Python: Retrieving only POP3 message text, no headers 2010-02-07

    I'm trying to make a Python program that retrieves only the body text of an email without passing headers or any other parameters. I'm not sure how to go about this. The goal is to be able to send basic commands to a program via message text. What I

  • regular expression to match specific text not linked 2010-02-10

    I would like to write a regular expression in javascript to match specific text, only when it is not part of an html link, i.e. match <a href="/link/page1">match text</a> would not be matched, but match text or <p>match text</p

  • how to add a color to a line in sharepoint 2007 list that has a specific text? 2010-03-11

    I would like to know how to add a color to a line in sharepoint 2007 list if in one field there is a specific text contained ? for example : I have a list that have three fields: list1 1.id 2.name 3.full description now i want to show only the first

  • How to select all anchor tags with specific text 2010-03-15

    Given multiple anchor tags: <a class="myclass" href="...">My Text</a> How do I select the anchors matching the class and with some specific text. eg Select all anchors with the class:'myclass' and text:'My Text' --------------Solutions---

  • To retrieve value from a text box in a form (view) to controller in cake php 2010-06-15

    How to o retrieve value from a text box in a form (view) to controller in cake php? --------------Solutions------------- Here's an example from the CakePHP book on saving your Model data: function edit($id) { //Has any form data been POSTed? if(!empt

  • Making specific text a hyperlink 2010-06-22

    is it possible to have specific text in a listbox line to act like a hyperlink? dim sLocation as string = "\\server\folder\subfolder\" LstOut.Items.Add("text text text" & sLocation) I would like this to open in explorer. This is not an ASP applic

  • select specific text on page and remove it 2010-08-16

    Is is possible to select specific text string on the page directly without a id, class, etc... I have this text string "ERROR: AffiliateID invalid" that I would like to remove from the page. Is it possible? --------------Solutions------------- This s

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.194 (s). 11 q(s)