I have a XML File which contains user ids and names. I want to extract all elements where both of these attributes have a value.
This is my xml
<pens>
<pen id="A">
<user name="USER1" email="USER1EMAIL" />
</pen>
<pen id="B">
<user name="USER2" email="USER2EMAIL" />
</pen>
<pen id="C"></pen>
<pen id="D">
<user name="USER3" email="USER3EMAIL" />
</pen>
</pens>
I need a out like this
A - USER1
B - USER2
D - USER3
I try it with XSL but I dont get it … can someone help please.
Thanks
When this XSLT:
…is applied against your original XML:
…the wanted result is produced:
Explanation:
We set the
@methodattribute of the<xsl:output>element to have a value oftext(since we are interested in text results).One template matches
<user>elements that have@nameand@emailattributes defined.When one such
<user>element is found, we output the value of its parent element’s@idattribute, a dash, the value of the element’s@nameattribute, and finally, a newline character.Saving this result to a file depends on which XSLT processor you are using. If you run your transformations via the command line (e.g., the way
xsltprocdoes), you can do something like this:$ xsltproc t.xsl input.xml > output.xml