如果子值等于另一个值,请更改XML孙值



我需要更正几百个XML文件。

假设文件的格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<MyData xmlns="urn:iso" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso">
<Hdr>
<AppHdr xmlns="urn:iso" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso">
<St>A</St>
<To>Z</To>
</Hdr>
<Data>
<Document xmlns="urn:iso" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso">
<CountryReport>
<RptHdr>
<RpDtls>
<Dt>2018-07-10</Dt>
</RpDtls>
</RptHdr>
<Country>
<Id>PT</Id>
<FullNm>Portugal</FullNm>>
<Bd>
<Tp>US</Tp>
</Bd>
</Country>
<Country>
<Id>ESP</Id>
<FullNm>Spain</FullNm>>
<Bd>
<Tp>EUR</Tp>
</Bd>
</Country>
</CountryReport>
</Document>
</Data>
</MyData>

我需要做的更换如下:

  • 如果国家ID是PT,我需要将Bd/Tp替换为"EUR">

我使用python尝试过使用sed、xmllint和ElementTrees的不同方法,但没有成功。

我可能使用了错误的xpath,但不幸的是我无法弄清楚。

你能帮忙吗?

实现目标的最简单方法是使用XSLT处理器。例如,使用调用Linux程序xsltproc或Windows/Linux程序saxon的脚本。

因为元素在命名空间中,所以必须为元素定义它。例如,将xmlns:ui="urn:iso"添加到xsl:stylesheet元素中,然后将以下模板与标识模板结合使用:

<xsl:template match="ui:Country[ui:Id='PT']/ui:Bd/ui:Tp">
<xsl:element name="Tp" namespace="{namespace-uri()}">EUR</xsl:element>
</xsl:template>

XSLT-1.0的身份模板为:

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template> 

使用XSLT-3.0,您可以使用以下指令:

<xsl:mode on-no-match="shallow-copy" />

因此,一个完整的XSLT-1.0文件来转换所有的XML文件可能看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ui="urn:iso">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>  
<xsl:template match="ui:Country[ui:Id='PT']/ui:Bd/ui:Tp">
<xsl:element name="Tp" namespace="{namespace-uri()}">EUR</xsl:element>
</xsl:template>
</xsl:stylesheet>

xsltprocbash命令可能看起来像

for file in *; do xsltproc transform.xslt $file > $file.NEW; done; 

最新更新