Reputation: 43
I'm trying to do something like an iteration over element names. I'm given Input in the form of
<list>
<element1>ID 1</element1>
<element2>name 1</element2>
<element3>town 1</element3>
<element4>ID 2</element4>
<element5>name 2</element5>
<element6>town 2</element6>
<!-- list continues like that -->
</list>
Now my target scheme is supposed to look like that:
<newlist>
<Person>
<ID>ID 1</ID>
<Name>name 1</Name>
<Town>town 1</Town>
</Person>
<Person>
<ID>ID 2</ID>
<Name>name 2</Name>
<Town>town 2</Town>
</Person>
<!-- more Persons here -->
</newlist>
The entries in the first list are always repeating in the same way, so what came to my mind is iterating over them and applying a modulo-operator to a loop-counter in order to figure out the right element the entry should be mapped to. However I could not find a Transformation like this. What would be the way to go here?
Upvotes: 1
Views: 933
Reputation: 117073
Try it this way?
XSLT 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="/list">
<xsl:for-each select="*[position() mod 3 = 1]">
<Person>
<ID>
<xsl:value-of select="."/>
</ID>
<Name>
<xsl:value-of select="following-sibling::*[1]"/>
</Name>
<Town>
<xsl:value-of select="following-sibling::*[2]"/>
</Town>
</Person>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2