1

I need a single XPath expression to get the values from multiple nodes and concatenate them with a '>' separator character.

<products>
    <product ID="6">
        <name>2-in-1 Microsoft Surface Pro 4</name>
        <properties>
            <property name="subcategories">
                <value>Microsoft</value>
            </property>
            <property name="subsubcategories">
                <value>Surface</value>
            </property>
            <property name="size">
                <value></value>
            </property>
        </properties>
        <variations/>
    </product>
</products>

I can get the value from a single node like so:

properties/property[@name='subcategories']

But how do I combine multiple nodes so the result would be:

"Microsoft>Surface" (or with a trailing ">" character if that's the only way)

I checked this post, but that uses a template and I want a single XPath expression (so I can filter on it in my ASP.NET application using the SelectSingleNode method).

update 1

I tried @splash58's suggestion: string-join(//property[@name="subcategories" or @name="subsubcategories"]/value, ">")

But then I get the error:

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

Dim responseString As String = "" 'this contains the XML document I load externally
productXML.LoadXml(responseString)
Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(productXML.NameTable)
mgr.AddNamespace("whatever", productXML.DocumentElement.NamespaceURI)

root = productXML.DocumentElement
nodeList = root.SelectNodes("/products/product")
For Each node In nodeList

    If node.SelectSingleNode("string-join(//property[@name=""subcategories"" or @name=""subsubcategories""]/value, "">"")") IsNot Nothing Then
        'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

    End If

Next node

How can I fix that error?

Community
  • 1
  • 1
Adam
  • 6,041
  • 36
  • 120
  • 208
  • 3
    As its name suggests `SelectSingleNode` will return a single node, which may be an element, or could be a text node. But it can't return a concatenated string value. It sounds like you need to use `SelectNodes`, and then do the concatenation of the selected nodes in your ASP.Net code instead. – Tim C Jun 10 '16 at 11:15
  • That's unfortunate, I was hoping an XSL expression could return a single value concatenated from multiple nodes....but thanks for clarifying :) – Adam Jun 10 '16 at 14:29
  • in Xpath 2 - `string-join(//property/value,'>')` returns "Microsoft>Surface>" – splash58 Jun 10 '16 at 14:52
  • @splash58: That looks like what I need, but is it also possible to define by name which properties specifically I need? In my case `subcategory` and `subsubcategory` – Adam Jun 10 '16 at 14:56
  • 1
    `string-join(//property[@name="subcategories" or @name="subsubcategories"]/value, ">")` – splash58 Jun 10 '16 at 15:04
  • @splash58: thanks. I now get an error, I added it in update 1...could you have another look? – Adam Jun 10 '16 at 16:36
  • semms like your library has not full support of xpath 2 – splash58 Jun 10 '16 at 17:13
  • @splash58 Looks like you're right: http://stackoverflow.com/questions/1525299/xpath-and-xslt-2-0-for-net Need some 3rd party library for that...thanks! – Adam Jun 10 '16 at 17:35
  • Unfortunately, no subject for thanking – splash58 Jun 10 '16 at 17:38

0 Answers0