Skip to content
Advertisement

Split string in xml

I have xml file which contains data .

<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>

I want to split only FATURANO Like that

6-R-7

and

4825

And this “4825” should have different name.

<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>

I Need convert xml data like that but if u answer me only with first question how to split string correctly i will do other transpormation

Advertisement

Answer

With single xmlstarlet expression:

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 
  • ededit mode

  • -xxpath expression

  • -a – append element

  • -t – the type of the appended element (in edit mode)

  • -n – the name of the appended element

  • -v – the value of the updated/appended element


The output:

<?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>
Advertisement