E
E
eliasum2022-01-31 14:26:22
Regular Expressions
eliasum, 2022-01-31 14:26:22

How to convert xml?

Input xml file:

<Item key="">
  <Adress>
  <Template key="01,07-10,21">
    <Channel>
    <Template key="1-5"/>
    </Channel>
  </Template>
  </Adress>
</Item>


Wrote a style file:
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="Template">
    <Item>
      <xsl:apply-templates select="@*|node()"/>
    </Item>
  </xsl:template>

</xsl:stylesheet>


The output xml file looks like this:
<Item key="">
  <Adress>
  <Item key="01,07-10,21">
    <Channel>
    <Item key="1-5" />
    </Channel>
  </Item>
  </Adress>
</Item>


How to modify the style file so that the output is:
<Item key="">
  <Adress>
  <Item key="01">
    <Channel>
    <Item key="1"/>
    <Item key="2"/>
    <Item key="3"/>
    <Item key="4"/>
    <Item key="5"/>
    </Channel>
  </Item>
  <Item key="07">
    <Channel>
    <Item key="1"/>
    <Item key="2"/>
    <Item key="3"/>
    <Item key="4"/>
    <Item key="5"/>
    </Channel>
  </Item>
  <Item key="08">
    <Channel>
    <Item key="1"/>
    <Item key="2"/>
    <Item key="3"/>
    <Item key="4"/>
    <Item key="5"/>
    </Channel>
  </Item>
  <Item key="09">
    <Channel>
    <Item key="1"/>
    <Item key="2"/>
    <Item key="3"/>
    <Item key="4"/>
    <Item key="5"/>
    </Channel>
  </Item>	
  <Item key="10">
    <Channel>
    <Item key="1"/>
    <Item key="2"/>
    <Item key="3"/>
    <Item key="4"/>
    <Item key="5"/>
    </Channel>
  </Item>
  <Item key="21">
    <Channel>
    <Item key="1"/>
    <Item key="2"/>
    <Item key="3"/>
    <Item key="4"/>
    <Item key="5"/>
    </Channel>
  </Item>
  </Adress>
</Item>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
roswell, 2022-01-31
@roswell

XSLT alone is not enough here (it is possible, however, only the effort will not be worth the result). It will be easier to take some scripting language with XSLT support, and wind up the Template / @ key parser and the XML generator on it according to the given conditions. It will be much faster than parsing strings by tokens .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question