-->

Update .properties file with values from .xml file

2019-08-31 10:52发布

问题:

Following Update .properties file with values from .xml file I have the following problems:

Problem 1: Example: X.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DOC SYSTEM "ts.dtd">
<?xml-stylesheet type="text/css" href="ts.css"?>
<DOC> 
    <PTXT ID="a.b.c.d" CONTEXT="label"><NTI>Text</NTI></PTXT>
</DOC>

Y.properties:

a.b.c.d=Text

and my output is:

a.b.c.d=
    Text=
=

Can you please help me as I really don't understand what's going on.

Problem 2: Example: X.xml

my.id = \u00D6ffnen Express WebTools

and Y.properties

<PTXT ID="my.id" CONTEXT="">Öffnen <NTI>Express WebTools</NTI></PTXT> 

results in: out.properties

my.id=Öffnen Express WebTools
my.id=\u00D6ffnen Express WebTools

instead of

my.id=Öffnen Express WebTools

Update

  • All my files contains nested NTI, no NTI and text with nested NTI combined.
  • I cannot join string that ends with = as janos suggested because my output is something like:

    first.id= second.id=Text from second id Text from first id= I don't know what's wrong with stackoverflow but it seems that it's not seeing my code as code....

回答1:

Given your example XML file, and this command:

xmlstarlet fo --dropdtd "$file" | xmlstarlet sel -t -m "/DOC/PTXT" -v $'concat(@ID, "=", ., "\n")'

I get this output:

a.b.c.d=
    Text

In the concat of the xmlstarlet if I change the . to ./NTI, I get the output:

a.b.c.d=Text

So I see two options for you. If some of your input files contain the NTI and other input files don't, and there are no input files with a mix of NTI present and absent, then you could add a conditional to detect whether the file contains the nested NTI or not, and use different xmlstarlet command accordingly. For example:

if xmlstarlet fo --dropdtd "$file" | xmlstarlet el | grep -q DOC/PTXT/NTI; then
    cmd=$'concat(@ID, "=", ./NTI, "\n")'
else
    cmd=$'concat(@ID, "=", ., "\n")'
fi

xmlstarlet fo --dropdtd "$file" | xmlstarlet sel -t -m "/DOC/PTXT" -v "$cmd"

If a single XML file may contain both structures, nested NTI and no nested NTI, then another option is to use $'concat(@ID, "=", ., "\n")', but process the output, joining lines that end with = with the next line.

Let me know if you need further help.