1

我正在寻找一个批处理脚本,它可以复制所有父元素,其子元素在 xml 文件中具有<![CDATA[....]]>并输出到新的 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#">.

预期输出为:output.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 文件。

4

1 回答 1

0

这可能会帮助您:

@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 用于 Windows

于 2013-09-11T18:07:16.497 回答