2

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 :( )

2
  • The same question: stackoverflow.com/questions/1403971/… Commented Oct 18, 2011 at 15:29
  • @Kan Thanks for the link, guess I'm stuck to using messy concats.... Commented Oct 18, 2011 at 15:38

2 Answers 2

4

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.

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

Comments

1

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.

1 Comment

Todd- thanks for the great answer, it was more elegant than what I have a the moment and I learned a few things. But this returns a node list and I needed a string, which in XPath 1.0 just takes the first node ("This is all one sentence"). Were you able to have all three returned as a string?

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.