Reputation: 14692
I don't think I can get away with one XPATH so it is just to illustrate the idea. I know I can write a simple python script but I'd prefer to use a tool, e.g. Oxygen (not xmlstarlet if possible!)
suppose I have the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<model>
<object name='obj1'>
<field type='int' name='fld1'/>
<field type='string' name='fld2'/>
</object>
</model>
I want names of all the int
fields. That's easy:
/model/object/field[@type='int']/@name
Now say I want to print the object name along with the field name. How can I do it?
I guess XSLT is the answer... trouble is, I hardly remember any of it and can't find in Oxygen how to play with it.
EDIT: expected output
obj1 fld1
obj2 fld7 (supposing I had them in the xml)
Upvotes: 0
Views: 415
Reputation: 14692
I ended up using the following XSL (perhaps it could be useful to somebody):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<body>
<h2>Integer Fields</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Object Name</th>
<th>Field Name</th>
</tr>
<xsl:for-each select="//object/field/[@type='int']">
<tr>
<td> <xsl:value-of select="parent::node()/@name"/> </td>
<td> <xsl:value-of select="@name"/> </td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 167716
As an alternative to the posted XPath 2.0 solution using for .. in
you can also use /model/object/field[@type='int']/concat(../@name, ':', @name)
.
Upvotes: 1
Reputation: 3428
In xpath 2.0 you could make following:
for $x in /model/object/field[@type = 'int'] return concat($x/@name, ' ', $x/../@name)
It returns
fld1 obj1
Upvotes: 2