我期待從大型XML文件中提取元素到單個文件,最好是使用命令或腳本。從大型XML提取整個元素到單個文件
問題是,XML格式不正確,並且是專有的,每當我嘗試使用XML工具(如twig或xmlstarlet)時,數據都會不合適地被淹沒,特殊字符會變得混亂。因此,我需要一個簡單的正則表達式匹配和的正是每場比賽所在的文件名重複說match1.xml match2.xml
示例XML源匹配到一個文件(反覆)直接複製:
...
<testcase id="001" kind="bvt">
<inputs>
<arg1>4</arg1>
<arg2>7</arg2>
</inputs>
<expected>11.00</expected>
</testcase>
<testcase id="002" kind="drt">
<inputs>
<arg1>9</arg1>
<arg2>6</arg2>
</inputs>
<expected>15.00</expected>
</testcase>
<testcase id="003" kind="bvt">
<inputs>
<arg1>5</arg1>
<arg2>8</arg2>
</inputs>
<expected>13.00</expected>
</testcase>
...
所需的輸出:match2.xml的
...
<testcase id="001" kind="bvt">
<inputs>
<arg1>4</arg1>
<arg2>7</arg2>
</inputs>
<expected>11.00</expected>
</testcase>
...
含量::
..
<testcase id="002" kind="drt">
<inputs>
<arg1>9</arg1>
<arg2>6</arg2>
</inputs>
<expected>15.00</expected>
</testcase>
...
match1.xml的 內容0
等等。
這裏有一些正則表達式,我會放在一起,將工作。我所需要的是幫助將一個循環放在一個bash腳本中,以將每個匹配/元素複製到它自己的文件中。
(<testcase*[\s\S]*?<\/testcase>)
這裏的大多數人可能會阻止使用shell/syntactic/regex工具的XML問題 - 熟悉xslt和使用例如[xalan](https://xalan.apache.org/xalan-j/),它具有在轉換過程中寫出不同文件的擴展名(其他處理器可能也有類似的功能)。如果你確定你的xml總是採用相同的格式,那麼我認爲它可以用gawk完成。 - 我們總是欣賞你看到的代碼,你嘗試但仍然有問題 –
XML是在一個專有格式,是不正確的,所以我非常確定,我將不得不使用sed和正則表達式來簡單地進行貪婪捕獲整個名稱空間並將每個文件複製到單個文件中。我一直使用像twig和xmlstarlett這樣的實用程序遇到的問題是數據變得繁瑣。以下是我在使用實用程序時必須做出的一些讓步。我在* .xml中爲 –
; do sed -i's/\ &/\&/ g'$ i;完成 爲i in * .xml; do sed -i's/\ &\; quot; \\'/ g'$ i;完成 爲i in * .xml; do-sec -i's/SOAP-ENC:arrayType =「xsd:string \ [1 \]」xsi:type =「SOAP-ENC:Array」/ xsi:type =「SOAP-ENC:Array」SOAP-ENC: arrayType =「xsd:string \ [1 \]」/ g'$ i;完成 爲i in * .xml; do sed -i's/xml:space =「preserve」xsi:type =「xsd:string」/ xsi:type =「xsd:string」xml:space =「preserve」/ g'$ i;完成 爲i in * .xml; do sed -i's/xml:space =「preserve」xsi:type =「cm:guid」/ xsi:type =「cm:guid」xml:space =「preserve」/ g'$ i;完成 爲i in * .xml; do sed -i's// g'$ 1;完成 –