批处理脚本,用于捕获其子元素具有 CDATA 并输出到 xml 文件的所有元素



我正在寻找一个批处理脚本,它可以复制其子元素具有<![CDATA[的所有父元素....]]> XML 文件中并输出到新的 XML 文件。

这是我的 xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<books xmlns:jndi="urn:jboss:jndi-binding-service:1.0">
    <jndi:bindings>
        <jndi:binding name="property/category/books/book1">
            <jndi:value type="java.lang.String">
                <![CDATA[
                  <?xml version="1.0" encoding="ISO-8859-1"?>
                  <urlConfig>
                    <defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
                    <urlKey name="logo" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                    <urlKey name="logo1" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                  </urlConfig>
                ]]>
            </jndi:value>
        </jndi:binding>
        <jndi:binding name="property/category/books/book2">
            <jndi:value type="java.lang.String">
                abc.com
            </jndi:value>
        </jndi:binding>
        <jndi:binding name="property/category/books/book3">
            <jndi:value type="java.lang.String">
                <![CDATA[
                    <?xml version="1.0" encoding="ISO-8859-1"?>
                    <urlConfig>
                      <defaults catID="1989" subcatID="1987" method="get" onError="keep"/>
                    <urlKey name="logo" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                    <urlKey name="logo1" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                  </urlConfig>
                ]]>
            </jndi:value>
        </jndi:binding>
        <jndi:binding name="property/category/books/book4">
            <jndi:value type="java.lang.String">
                <![CDATA[
                <?xml version="1.0" encoding="ISO-8859-1"?>
                <urlConfig>
                      <defaults catID="1990" subcatID="1987" method="get" onError="keep"/>
                    <urlKey name="logo" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                    <urlKey name="logo1" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                  </urlConfig>
                ]]>
            </jndi:value>
        </jndi:binding>
        <jndi:binding name="property/category/books/book5">
            <jndi:value type="java.lang.String">
                abc.com
            </jndi:value>
        </jndi:binding>
    </jndi:bindings>
</books>

因此,预期的输出必须捕获其子元素具有<![CDATA[的所有父元素...]]> .在此示例中,父元素<jndi:binding name="property/category/books/book#">

预期输出为:输出.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<books xmlns:jndi="urn:jboss:jndi-binding-service:1.0">
    <jndi:bindings>
        <jndi:binding name="property/category/books/book1">
            <jndi:value type="java.lang.String">
                <![CDATA[
                  <?xml version="1.0" encoding="ISO-8859-1"?>
                  <urlConfig>
                    <defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
                    <urlKey name="logo" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                    <urlKey name="logo1" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                  </urlConfig>
                ]]>
            </jndi:value>
        </jndi:binding>
        <jndi:binding name="property/category/books/book3">
            <jndi:value type="java.lang.String">
                <![CDATA[
                    <?xml version="1.0" encoding="ISO-8859-1"?>
                    <urlConfig>
                      <defaults catID="1989" subcatID="1987" method="get" onError="keep"/>
                    <urlKey name="logo" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                    <urlKey name="logo1" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                  </urlConfig>
                ]]>
            </jndi:value>
        </jndi:binding>
        <jndi:binding name="property/category/books/book4">
            <jndi:value type="java.lang.String">
                <![CDATA[
                <?xml version="1.0" encoding="ISO-8859-1"?>
                <urlConfig>
                      <defaults catID="1990" subcatID="1987" method="get" onError="keep"/>
                    <urlKey name="logo" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                    <urlKey name="logo1" altURL="def.com">
                      <address>abc.com</address>
                    </urlKey>
                  </urlConfig>
                ]]>
            </jndi:value>
        </jndi:binding>
    </jndi:bindings>
</books>
我使用 xml 处理器,xslt 来处理 xml 文件,

但不知何故,如果文件中包含 CDATA 部分,则 xslt 处理器不容易处理 xml 文件。

这可能会对您有所帮助:

@ECHO OFF &SETLOCAL
set "infile=file.xml"
set "outfile=file.new"
rem IMPORTANT! Put a [tab] and a [space] behind the colon to the delims.
for /f "tokens=1-3delims=<>:     " %%a in ('findstr /nc:"jndi:binding" /c:"![CDATA[" /c:"/jndi:binding" "%infile%"') do (
    if "%%~b:%%~c"=="jndi:binding" set "start=%%a"
    if "%%~b"=="![CDATA[" set "found=%%a"
    if "%%~b:%%~c"=="/jndi:binding" (
        if defined found call set "line=%%line%%%%start%%,%%ap;"
        set "start="
        set "found="
    )
)
sed -n "%line%" "%infile%">"%outfile%"
type "%outfile%"

适用于视窗的 sed

最新更新