I want to transform Third-Party xml files to a csv file with xmlstarlet. Some of the files use default namespace with a xmlns declaration, others use default namespace without xmlns declaration and other use a strict default namespace in most precise interpretation.
Here are smaller files that should clarify my problem.
foo1.xml
<?xml version="1.0"?>
<root xmlns="http://my.namespace" xmlns:fooNS="http://foo.namespace" xmlns:barNS="http://bar.namespace">
<fooNS:foo id="1">FOO 1</fooNS:foo>
<fooNS:foo id="2">FOO 2</fooNS:foo>
<barNS:bar ref="2" unitRef="Unit1">2000</barNS:bar>
<unit id="Unit1">
<measure>bars</measure>
</unit>
</root>
foo2.xml:
<?xml version="1.0"?>
<root xmlns:fooNS="http://foo.namesapece" xmlns:barNS="http://bar.namespace">
<fooNS:foo id="1">FOO 1</fooNS:foo>
<fooNS:foo id="2">FOO 2</fooNS:foo>
<barNS:bar ref="2" unitRef="Unit1">2000</barNS:bar>
<unit id="Unit1">
<measure>bars</measure>
</unit>
</root>
foo3.xml
<?xml version="1.0"?>
<myNS:root xmlns:myNS="http://my.namespace" xmlns:fooNS="http://foo.namesapece" xmlns:barNS="http://bar.namespace">
<fooNS:foo id="1">FOO 1</fooNS:foo>
<fooNS:foo id="2">FOO 2</fooNS:foo>
<barNS:bar ref="2" unitRef="Unit1">2000</barNS:bar>
<unit id="Unit1">
<measure>bars</measure>
</unit>
</myNS:root>
Now I want a file with "FOO 2 | 2000 | bars"
as output. Attribute "unitRef" is defined as IDREF in the xsd.
This command works for foo1.xml (but NOT for foo2.xml and foo3.xml):
$> xmlstarlet sel -N xbrli="http://my.namespace" \
-t -m "//fooNS:foo[../barNS:bar/@ref = @id]"
-v . -o " | " \
-v "../barNS:bar[@ref=current()/@id]" -o " | " \
-v \
"//xbrli:unit[@id=current()/../barNS:bar[@ref=current()/@id]/@unitRef]/xbrli:measure" \
-n foo1.xml
And this command works for foo2.xml AND foo3.xml (but NOT for foo1.xml):
$> xmlstarlet sel -N xmlns="http://my.namespace" \
-t -m "//fooNS:foo[../barNS:bar/@ref = @id]" \
-v . -o " | " \
-v "../barNS:bar[@ref=current()/@id]" -o " | " \
-v \
"//unit[@id=current()/../barNS:bar[@ref=current()/@id]/@unitRef]/measure" \
-n foo[23].xml
Question: is there a syntax that works for all three third-party files ? If not with xmlstarlet, then maybe with a xslt file? Or maybe it's possible to process all xml file (with xmlstarlet or xslt) so that they act similarly?
Thanks.