简体   繁体   English

根据特定值,使用正则表达式或特定软件更改 XML 文件上的值

[英]Changing values using regex or a certain software, depending on a certain value, on a XML file

I got an XML file with around 1000 codes, each code has the following structure:我得到了一个包含大约 1000 个代码的 XML 文件,每个代码具有以下结构:

<codes>
        <code name="901" description="CONIFERADENSA BORDE" tags="Poligonos" type="1" command="" helpFile="" table="ARBOLADO" CONDITIONS="id_tipo=0122,IDIOMA=0,COMPONEN1D=2,DENSIDAD=1,TIPO_0122=1,PLANT_0122=2" enabled="1" print="1" streamMode="0">
            <ccc:Triggers>
            </ccc:Triggers>
            <representation style="continuous" color="77" color-stereo="219" width="1" width-stereo="1" print-color="0" print-width="0.010000" font="0" italic="0" />
            <io>
                <iobin:transform code="901"/>
                <iobind:transform code="901"/>
                <iocsv:transform type="0" />
                <iodgn:transform level="54" colorEntity="2" styleEntity="0" weightEntity="1" gg="901" class="0" color="2" style="0" weight="1" >
                    <iodgn:point type="POINT" cell="" sx="1.000000" sy="1.000000" />
                    <iodgn:line type="LINESTRING" />
                    <iodgn:text font="STANDARD" />
                </iodgn:transform>
                <iodwg:transform layer="901">
                    <iodwg:line type="Polyline3D"/>
                    <iodwg:point type="Point"/>
                    <iodwg:text style="Standard" oblique="0.000000" />
                </iodwg:transform>
                <ioGeomedia:transform table="CUBIERTATER_LIN" textHeight="1.000000" />
                <iokml:transform type="0" elevation="0" extrude="0" opacity="255" />
                <ioshp:transform nameSHP="CUBIERTATER_LIN" typeNewTable="13" />
                <iosvg:transform stroke-width="1.000000" stroke="#44382b" stroke-linejoin="miter" stroke-linecap="butt" fill="0" fill-color="#44382b" point-size="1.000000" />
            </io>
        </code>

what i need to do is to change the ioshp:transform.nameSHP value with the same value that is on the code.table, so if the example has the code.table value equal to "Arbolado", the ioshp:tansform.nameSHP needs to be also "Arbolado".我需要做的是将 ioshp:transform.nameSHP 值更改为 code.table 上的相同值,因此如果示例的 code.table 值等于“Arbolado”,则 ioshp:tansform.nameSHP 需要也是“Arbolado”。

Could you help me with this?你能帮我解决这个问题吗? i'm trying to use regex on notepad++ to make it quicker, but i'm being uncapable so far...我正在尝试在记事本 ++ 上使用正则表达式以使其更快,但到目前为止我无法做到......

You need to use re.sub regex:您需要使用 re.sub 正则表达式:

>>> import re
>>> re.sub(r"(<code[^>]*table=\"(ARBOLADO)\"[^>]*>.*?<ioshp\:transform\s+nameSHP=\")(?!ARBOLADO).*?(\"[^>]*>.*?<\/code>)", r"\1\2\3", YOUR_STRING)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM