i have an XML-file that i want to clean up with an XLST-file and save it as an new XML-file.
I am still a beginner but like to learn more. I have used a "for-each" function but i cant find what i am doing wrong.
He loops the file, but i get a repeating <sub> but i need all the <sub> elements
this is part of my original.xml:
<?xml version="1.0" encoding="UTF-8"?>
<dataroot>
<index>
<title>title 1</title>
<sub>
<subtitle>subtitle1</subtitle>
<number>11111</number>
<description>description1</description>
<price>price1</price>
<image>12345</image>
<number>22222</number>
<description>description2</description>
<price>price2</price>
<image>12345</image>
<number>33333</number>
<description>description3</description>
<price>price3</price>
<image>12345</image>
<subtitle>subtitle2</subtitle>
<number>4444</number>
<description>description4</description>
<price>price4</price>
</sub>
<sub>
<subtitle>subtitle3</subtitle>
<number>55555</number>
<description>description5</description>
<price>price5</price>
<image>12345</image>
</sub>
</index>
</dataroot>
This is my XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<index>
<xsl:for-each select="//sub">
<title>
<xsl:value-of select="//title"/>
</title>
<sub>
<subtitle>
<xsl:value-of select="//subtitle"/>
</subtitle>
<number>
<xsl:value-of select="//number"/>
</number>
<description>
<xsl:value-of select="//description"/>
</description>
<image>
<img>
<xsl:attribute name="href">
<xsl:value-of select="//image"/>
</xsl:attribute>
</img>
</image>
</sub>
</xsl:for-each>
</index>
</xsl:template>
</xsl:stylesheet>
This is the result:
<?xml version="1.0" encoding="UTF-8"?>
<index>
<title>title 1</title>
<sub>
<subtitle>subtitle1</subtitle>
<number>11111</number>
<description>description1</description>
<image>
<img href="12345"/>
</image>
</sub>
<title>title 1</title>
<sub>
<subtitle>subtitle1</subtitle>
<number>11111</number>
<description>description1</description>
<image>
<img href="12345"/>
</image>
</sub>
<title>title 1</title>
<sub>
<subtitle>subtitle1</subtitle>
<number>11111</number>
<description>description1</description>
<image>
<img href="12345"/>
</image>
</sub>
<title>title 1</title>
<sub>
<subtitle>subtitle1</subtitle>
<number>11111</number>
<description>description1</description>
<image>
<img href="12345"/>
</image>
</sub>
</index>
What i want is:
<?xml version="1.0" encoding="UTF-8"?>
<index>
<title>title 1</title>
<sub>
<subtitle>subtitle1</subtitle>
<number>11111</number>
<description>description1</description>
<image>
<img href="12345"/>
</image>
<number>22222</number>
<description>description2</description>
<image>
<img href="12345"/>
</image></sub>
<number>33333</number>
<description>description3</description>
<image>
<img href="12345"/>
</image>