我有包含数据的XML文件。

<STOKUNIT>
  <BELGETARIGI>2017-09-20</BELGETARIGI>
  <STOKKODU>1KKUPS0098</STOKKODU>
  <STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
  <RENK>139</RENK>
  <BEDEN>.</BEDEN>
  <STOKBARKODU>8680044399128</STOKBARKODU>
  <FATURANO>6-R-7-4825</FATURANO>
  <MAGAZAKODU>MGUR-02</MAGAZAKODU>
  <MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
  <MIKTAR>1.00</MIKTAR>
  <SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
  <SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
  <KDVTUTAT>4.71</KDVTUTAT>
  <FIYAT>30.89</FIYAT>
  <TUTAR>30.89</TUTAR>
  <INDIRIMTUTAR>0.00</INDIRIMTUTAR>
  <BIRIMFIYAT>30.89</BIRIMFIYAT>
  <NETTUTAR>30.89</NETTUTAR>
  <ODEMETIPI> Nakit</ODEMETIPI>
  <ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>

我只想那样分开法图拉诺
6-R-7号

4825个
这个“4825”应该有不同的名字。
<STOKUNIT>
          <BELGETARIGI>2017-09-20</BELGETARIGI>
          <STOKKODU>1KKUPS0098</STOKKODU>
          <STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
          <RENK>139</RENK>
          <BEDEN>.</BEDEN>
          <STOKBARKODU>8680044399128</STOKBARKODU>
          <FATURANO>6-R-7</FATURANO>
          ***<NEWONE>4825</NEWONE>***
          <MAGAZAKODU>MGUR-02</MAGAZAKODU>
          <MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
          <MIKTAR>1.00</MIKTAR>
          <SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
          <SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
          <KDVTUTAT>4.71</KDVTUTAT>
          <FIYAT>30.89</FIYAT>
          <TUTAR>30.89</TUTAR>
          <INDIRIMTUTAR>0.00</INDIRIMTUTAR>
          <BIRIMFIYAT>30.89</BIRIMFIYAT>
          <NETTUTAR>30.89</NETTUTAR>
          <ODEMETIPI> Nakit</ODEMETIPI>
          <ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>

我需要像那样转换XML数据,但如果你只回答第一个问题如何正确拆分字符串,我将做其他转换。

最佳答案

单个xmlstarlet表达:

xmlstarlet ed -u '//STOKUNIT/FATURANO' -x "substring(., 0,string-length(.)-4)" \
-a '//STOKUNIT/FATURANO' -t elem -n NEWONE \
-v "$(xmlstarlet sel -t -v 'substring(//STOKUNIT/FATURANO, string-length(//STOKUNIT/FATURANO)-3)' file.xml)" file.xml

ed-编辑模式
-x-xpath表达式
-a-附加元素
-t-附加元素的类型(在编辑模式下)
-n-附加元素的名称
-v-更新/附加元素的值
输出:
<?xml version="1.0"?>
<STOKUNIT>
  <BELGETARIGI>2017-09-20</BELGETARIGI>
  <STOKKODU>1KKUPS0098</STOKKODU>
  <STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
  <RENK>139</RENK>
  <BEDEN>.</BEDEN>
  <STOKBARKODU>8680044399128</STOKBARKODU>
  <FATURANO>6-R-7</FATURANO>
  <NEWONE>4825</NEWONE>
  <MAGAZAKODU>MGUR-02</MAGAZAKODU>
  <MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
  <MIKTAR>1.00</MIKTAR>
  <SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
  <SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
  <KDVTUTAT>4.71</KDVTUTAT>
  <FIYAT>30.89</FIYAT>
  <TUTAR>30.89</TUTAR>
  <INDIRIMTUTAR>0.00</INDIRIMTUTAR>
  <BIRIMFIYAT>30.89</BIRIMFIYAT>
  <NETTUTAR>30.89</NETTUTAR>
  <ODEMETIPI> Nakit</ODEMETIPI>
  <ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>

关于xml - 在xml中分割字串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46979876/

10-11 22:43