1

使用复制时如何使 XmlStarlet 保留 CDATA?<![CDATA[ ]]>必须维护,因为生成(和使用)数据的应用程序坚持使用 CDATA 指令围绕某些数据。

示例.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RSLogix5000Content SchemaRevision="1.0" SoftwareRevision="20.01" >
    <Controller Use="Target" Name="SOME_TARGET_NAME" ProcessorType="1789-L60">
        <Tags>
            <Tag Name="gstrScrap" TagType="Base" DataType="STRING" Constant="false" ExternalAccess="Read/Write">
                <Data>05 00 00 00 53 43 52 41 50 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00</Data>
                <Data Format="String" Length="5">
                    <![CDATA['SCRAP']]>
                </Data>
            </Tag>
        </Tags>
    </Controller>
</RSLogix5000Content>

使用命令;

xml sel -t -c "RSLogix5000Content/Controller/Tags/Tag" Example.xml

生成应用程序无法处理的数据,因为<![CDATA[ ]]>已从'SCRAP'周围删除;

<Tag Name="gstrScrap" TagType="Base" DataType="STRING" Constant="false" ExternalAccess="Read/Write">
                                <Data>05 00 00 00 53 43 52 41 50 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00</Data>
                                <Data Format="String" Length="5">
                                        'SCRAP'
                                </Data>
                        </Tag>
4

1 回答 1

0

干得好:

xml sel -t -c "RSLogix5000Content/Controller/Tags/Tag" Example.xml >t1.xml
xml ed -i "/Tag/Data/text()" -t text -n "" -v "<![CDATA[" t1.xml >t2.xml
xml ed -a "/Tag/Data/text()" -t text -n "" -v "]]>" t2.xml >t3.xml
sed -r "s/&lt;!\[CDATA\[/<![CDATA[/g; s/\]\]&gt;/]]>/g" t3.xml >t4.xml

除非您在 xml 中转义了字符,否则您不需要sed :

xml sel -t -c "RSLogix5000Content/Controller/Tags/Tag" Example.xml >t1.xml
xml ed -i "/Tag/Data/text()" -t text -n "" -v "<![CDATA[" t1.xml >t2.xml
xml ed -a "/Tag/Data/text()" -t text -n "" -v "]]>" t2.xml >t3.xml
xml unesc <t3.xml >t4.xml
于 2013-03-27T20:43:12.680 回答