1

I have XPath expression //@*[contains(., 'multiDataSeries')] it is giving the nodes of which have attributes containing multiDataSeries. How do I find the just tag name of these nodes?

For example, it is giving the below results.

<processParameter name="multiDataSeries"> <lastModified isNull="true"/>  </processParameter>

<processVariable name="multiDataSeriesP">  </processVariable>

<ns17:dataOutput xmlns:ns17="http://www.omg.org/spec/BPMN/20100524/MODEL" name="multiDataSeries" itemSubjectRef="itm.12.63a604e8-e026-4605-aae1-272b67822cc7" isCollection="true" id="2055.ad3b4033-e152-4060-871d-a360c3f21226"/>

<ns17:dataObject xmlns:ns17="http://www.omg.org/spec/BPMN/20100524/MODEL" itemSubjectRef="itm.12.63a604e8-e026-4605-aae1-272b67822cc7" isCollection="true" name="multiDataSeriesP" id="2056.401acef4-2a08-4d7a-a34f-5642cc73a329">  </ns17:dataObject>

My goal is to find just the tagNames in a list like below.

processParameter 
processVariable   
ns17:dataOutput
ns17:dataObjec
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Harishfysx
  • 81
  • 1
  • 7
  • Does this answer your question? [Getting element's name in XPATH](https://stackoverflow.com/questions/7984508/getting-elements-name-in-xpath) – Geshode May 27 '21 at 11:41
  • No..its not working!. I tried "name(//@*[contains(.,'multiDataSeries')])" – Harishfysx May 27 '21 at 11:48

3 Answers3

1

If - as it seems - you are limited to XSLT 1.0, you can use:

<xsl:for-each select="//*[@*[contains(., 'multiDataSeries')]]">
    <xsl:value-of select="name()"/>
    <xsl:text>&#10;</xsl:text>
</xsl:for-each>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

Your current XPath addresses the attributes. I would change it to look for elements * and move the attribute criteria into a predicate, and then use the name() function to get the element's names:

//*[@*[contains(., 'multiDataSeries')]]/name()
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • It is giving the result as expected when I tried in "xpather.com" with this message. Non-standard output. But when I plugged it in my code (node js). its saying throw new Error("XPath parse error"); ^ Error: XPath parse error at XPathParser.parse – Harishfysx May 27 '21 at 11:54
  • node.js XPath is likely 1.0, which means you can't use the `name()` function on an axis path. You could wrap the XPath selecting the elements in the name function, but then you would only get the first one. You may need to select all of the elements and then iterate over them and obtain their names. – Mads Hansen May 27 '21 at 12:13
  • 1
    With Node.js you could install saxon-js plus xslt3 from npm and then you would have XPath 3.1 support. – Martin Honnen May 27 '21 at 12:14
  • ^^Then you should definitely do that! Use the full power of the latest XPath and you will have a much easier time. – Mads Hansen May 27 '21 at 12:31
  • Thank you all for your help!! – Harishfysx May 28 '21 at 17:23
0

As suggested by @MartinHonnen in the comments of MadsHansen answer and use Saxon-Js with node.js

And if your xml-results in your question are representative, meaning its's always the @name attribute and always starts with 'multiDataSeries', you could make the code perform better with:

//*[@name[starts-with(., 'multiDataSeries')]]/name() 
Siebe Jongebloed
  • 3,906
  • 2
  • 14
  • 19