Assuming that the XML file is well formed:
<?xml version="1.0"?>
<header>
<prop-group name="ora_reconstruction">
<prop prop-type="TemplateCode">XXPO_PHYS_INV_ADJ_ES_AS</prop>
<prop prop-type="extractorVersion">10.1.3.4.2_1.5</prop>
</prop-group>
</header>
Using XMLStarlet:
xmlfile='ES - Physical Inventory Adjustment Report US PDF_es_ES.xlf'
LOBCODE=$( xml sel -t -v '//prop[@prop-type="TemplateCode"]' "$xmlfile" )
This uses XMLStarlet to extract the value of the prop
node whose prop-type
attribute is TemplateCode
. The variable LOBCODE
will get the value XXPO_PHYS_INV_ADJ_ES_AS
with the above XML.
XMLStarlet may sometimes be installed as xmlstarlet
rather than as xml
.
There are two issues with your code:
You redirect the output of the sed
command to a file whose name is given by $LOBCODE
. If $LOBCODE
is empty, then this redirection will fail. What I assume you wanted to do was to assign the output of sed
to the variable LOBCODE
. This is done with a command substitution as I've shown above. Note that sed
is a poor choice of tool for parsing XML data.
You instruct sed
to work with a file called filename
. To have sed
work on the value of the variable filename
you will need to use $
in front of the variable name. Also, since the filename has spaces in it, you need to double quote the variable expansion (you should always do this, regardless). Hence you should use "$filename"
(I've used a more descriptive variable name in my code above).
.xlf
file than just what you have there, correct? Otherwise, it's malformed XML and the tag isn't really appropriate.