I am sure this should be easier than I'm making it but I'm trying to get the following response from my source. Please could somebody help me with the xpath?
I am trying to select the line elements with the values "extract". In real world, I'm reading a text file and using xml and want to pick out the lines which read extract.
Source
<line>
<line number="1">blah</line>
<line number="2">extract</line>
<line number="3">blah</line>
<line number="4">extract</line>
</line>
Required Response:
<line>
<line number="2">extract</line>
<line number="4">extract</line>
</line>
or even just
extract
extract
would be fine.
Many Thanks,
Use:
/*/*[.='extract']/text()
or even:
//text()[.='extract']
This (use = if you use XPath 1.0):
/line/line[text() eq 'extract']/text()
will give you: (See evaluation)
extract
extract
The reason why //line is not a good idea, is that you use line tags for 2 things (your top tag line and your childs of line) - you should not do that :-/. If you want the elements instead of the text, you can write:
/line/line[text() eq 'extract']
And last, this will give you the count (it seems it is what you really need?)
count(/line/line[text() eq 'extract'])
Since your subject mentions attributes (even if your question doesn't) I assume you want to find the value of the number attribute for all matching lines. If so:
/line/line[text()='extract']/#number
//line[contains(text(), 'extract')]
EDIT: This works for XPath 1.0, and it obviously searches for the string (rather than requiring equality).
Related
Is there a way to generate a random number with xpath? My input is any well-formed xml, the output should be a random integer of a given length.
I usually achieve it with any coding or xslt but I'm struggling to find a working xpath expression.
XPath 3.1 has a function fn:random-number-generator().
In earlier XPath versions you'll need to improvise.
When asking XPath questions please say which version you are using - the ancient XPath 1.0 is still in widespread use so it's impossible to make guesses.
Since this question is the top google result, I'll say in XPath 3.1, Mr. Kay's answer can be achieved like this:
Say you want a random element. Count the elements:
<xsl:variable name="random-upper-limit" select="count(/myroot/mypath/myelement)"/>
Then get a random index number:
<xsl:variable name="my-random-number" select="random-number-generator()['next']?permute(1 to $random-upper-limit)[1]"/>
I'm trying to count the amount of colon followed by space within tag "report":
<report>But that doesn't work either: what do you guys think: To be solved</report>
My xpath code
`report[count([text()=": "]) > 1]`
but it not works
To complete the XPath 2.0 solution provided by #Slkrasnodar, here's the XPath 1.0 solution :
string-length(//report)-string-length(translate(//report,":",""))
We check the length of the original string. Then we generate a string without colon and check its length. Finally, we substract this length from the original length string to get the result.
Output : 2
EDIT : Please fix your XML file. It contains unclosed tags. I've worked with :
<authors>
<author>
<gnm>Lee</gnm>
<snm>Auch</snm>
</author>
<report>Search Results: Count the number of words in a xml node using xsl: Thank you</report>
</authors>
To get the authors names who have a report which contains more than one colon, use the following expression :
//authors[string-length(//report)-string-length(translate(//report,":",""))>1]//gnm/text()
Output : Lee
I am new to xpath expression. Need help on a issue
Consider the following Document :
<tbody><tr>
<td>By <strong>Bec</strong></td>
<td><strong>Great Support</strong></td>
</tr></tbody>
In this I have to find the text inside tags separately.
Following is my xpath expression:
//tbody//td//strong/text();
It evaluates output as expected:
Bec
Great Support
How can I write xpath expressions to distinguish between the results i.e Becand Great Support
It's rather unclear what you're trying to do, but the following should succeed in selecting them separately:
//tbody/tr/td[1]/strong
and
//tbody/tr/td[2]/strong
Note that the text() you had at the end is most likely not needed in this case.
Not sure I understand 100%, but if you're trying to get the text of the first and the second strong tags, you can use position (1 based index)
//tbody/td[position()=1]/strong/text() //first text
//tbody/td[position()=2]/strong/text() //second text
This solution only applies to the current sample though, where your strong tags are inside either the first or second td tag.
Not sure this is what you're looking for... anyway, assuming you're asking to retrieve a node based on its text you can look up for text content by doing something like:
//tbody//td//strong/text()[.="Bec"]
PS
in [.=""] the dot is an alias for text() self::node() (thanks JLRishe for pointing out the mistake).
Using xpath, I want to return the value 000078 & 000077 from the below xml. The text for "Entity" tag can be 2 comma separated values or 3 or more. I always want the last value.
<Parent ID="123">
<SubParent ID="1">
<Name>Modem</Name>
<Entity>000006,000069,000078</Entity>
</SubParent>
<SubParent ID="2">
<Name>Modem</Name>
<Entity>000006,000077</Entity>
</SubParent>
</Parent>
XPath is a selection language, not a string processing (or general purpose programming) language, and you can only select from the distinct nodes in your document.
The nodes that contain the values you are looking for are two text nodes, '000006,000069,000078' and '000006,000077', so //Entity/text() (or //Entity) is the closest you can get with XPath alone.
Any further string processing, like pulling out the substring after the last comma, must be done in the host language.
This is one of the examples that show that storing opaque strings that contain multiple data points (like comma-separated values) in XML is a bad idea.
This is how your XML should look like.
<Parent ID="123">
<SubParent ID="1">
<Name>Modem</Name>
<Entity>000006</Entity>
<Entity>000069</Entity>
<Entity>000078</Entity>
</SubParent>
<SubParent ID="2">
<Name>Modem</Name>
<Entity>000006</Entity>
<Entity>000077</Entity>
</SubParent>
</Parent>
because now you would easily be able to select //Entity[last()]/text() and get exactly two nodes.
Hello I need use Cyrillic chars inside of UrlRewriting.config
Does any one know how to do it in correct way ?
If i write Cyrillic chars directly I got error. if I use code like %D1 %82 %D1 %83 it doesn't work.
Thanks for advise.
If you need to use Cyrillic characters in the config file, use the character entity code instead. This way, you won't need to fiddle around with the encoding of the config file. I don't know Cyrillic so I'm just going to use a random string of characters for demonstration.
For example, if you want to change the url ~/БДЯ/Л.aspx to ~/new-БДЯ/Л.aspx, apply the following:
<add name="datafolderrewrite"
virtualUrl="^~/БДЯ/(.*).aspx"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/new-БДЯ/$1.aspx"
ignoreCase="true" />
I used the following page to detemine the respective entity codes: http://tlt.its.psu.edu/suggestions/international/bylanguage/cyrillicchart.html