Reputation: 3219
So say I have a node structure like:
<Row>
<Column Name="Primary Number">1</Column>
<Column Name="Secondary Number">01</Column>
<Column Name="Text">This is all one sentence</Column>
<Row>
<Column Name="Primary Number">1</Column>
<Column Name="Secondary Number">02</Column>
<Column Name="Text">I would like to return</Column>
<Row>
<Column Name="Primary Number">1</Column>
<Column Name="Secondary Number">03</Column>
<Column Name="Text">as one string, in one xpath line</Column>
First of all I would like to say this is not my xml, nor did I help construct it. But I am forced to use it to retrieve the info I need....So please help.
So what I would like to do using one XPath, is to return one string containing the text from all n number of Text fields of all Primary Number=1 nodes.
Is this possible using XPath 1.0?
I have a not-so elegant solution, but it only works if I know the exact number of secondary numbers, and then it's just a bunch of messy concat()'s...
Hello all, and thanks for everyone's response! So what I wanted to do with this string, was use contains() to search for specific keywords, but I found a shortcut around actually having a concatenated string of all the values to do so, and just had to share it in case someone had a similar problem. Tod's hint helped quite a bit with this, I just added a conditional to it and BAM magic. Here it is:
Table[@Name='TableName']/Row/Column[@Name='Text' and contains(text(),"keyword")][preceding-sibling::Column[@Name='Primary Number' and text()='15']]
So what this does is instead of concatenating all of the node value results as one string then using contains() on it, it searches each line for the word separately and thus removes the need for the impossible. Thanks for all the help guys, couldn't have done it without all the great responses! (I'd give everyone vote ups, but I'm too new :( )
Upvotes: 2
Views: 4152
Reputation: 163438
This is done in XPath 2.0 by the string-join() function. It can be done in XSLT 1.0 using <xsl:for-each>
. But I don't think it can be done in pure XPath 1.0 without host-language support.
Upvotes: 4
Reputation: 11337
This seems to be working here:
//Row/Column[@Name="Text"][preceding-sibling::Column[@Name="Primary Number"]/text()="1"]
returns:
This is all one sentence
I would like to return
as one string, in one xpath line
Although, I'm not sure if you find the new lines in the result string acceptable.
Upvotes: 1