使用 .xml 文件中的值更新 .properties 文件 - 问题 [英] Update .properties file with values from .xml file - problems

查看:70
本文介绍了使用 .xml 文件中的值更新 .properties 文件 - 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循 使用 .xml 文件中的值更新 .properties 文件 我有以下问题:

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

问题 1:示例: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:

Y.properties:

a.b.c.d=Text

我的输出是:

a.b.c.d=
    Text=
=

你能帮我吗,因为我真的不明白发生了什么.

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

问题 2:示例:X.xml

my.id = \u00D6ffnen Express WebTools

和 Y.properties

and Y.properties

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

结果:out.properties

results in: out.properties

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

代替

my.id=Öffnen Express WebTools

更新

  • 我的所有文件都包含嵌套 NTI,没有 NTI 和嵌套 NTI 组合的文本.
  • 我无法按照 janos 的建议加入以 = 结尾的字符串,因为我的输出类似于:

  • 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=来自第二个 id 的文本来自第一个 id= 的文本我不知道 stackoverflow 有什么问题,但它似乎没有将我的代码视为代码......

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....

推荐答案

鉴于您的示例 XML 文件,以及此命令:

Given your example XML file, and this command:

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

我得到这个输出:

a.b.c.d=
    Text

xmlstarletconcat 中,如果我将 . 更改为 ./NTI,我会得到输出:

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

a.b.c.d=Text

所以我为您提供了两个选项.如果您的某些输入文件包含 NTI 而其他输入文件不包含,并且没有混合存在和不存在 NTI 的输入文件,那么你可以添加一个条件来检测文件是否包含嵌套的 NTI ,并相应地使用不同的 xmlstarlet 命令.例如:

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"

如果一个 XML 文件可能包含这两种结构,嵌套 NTI 和无嵌套 NTI,那么另一种选择是使用 $'concat(@ID, "=", ., "\n")',但是处理输出,将以 = 结尾的行与下一行连接起来.

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.

这篇关于使用 .xml 文件中的值更新 .properties 文件 - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆