Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Xslt tokenize help

8 views
Skip to first unread message

johanl...@gmail.com

unread,
Feb 17, 2013, 6:26:31 AM2/17/13
to
Hello is it possible to use xslt to go from

Code:
<XML>
<Name>tag1value1;tag1value2</Name>
<Adress>tag2value1;tag2value2</Adress>
</XML>
To this

Code:
<XML>
<LINE>
<Name>tag1value1</Name>
<Adress>tag2value1</Adress>
</LINE>
<LINE>
<Name>tag1value2</Name>
<Adress>tag2value2</Adress>
</LINE>
</XML>
Any help would be most appreciated.

Johan

Peter Flynn

unread,
Feb 17, 2013, 3:59:30 PM2/17/13
to
Provided you can guarantee that the content of Name and Adress are
perfectly aligned (same number of tokens, always delimited by
semicolons) then this will work:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="XML">
<XML>
<xsl:apply-templates/>
</XML>
</xsl:template>

<xsl:template match="Name">
<xsl:variable name="addresses"
select="tokenize(following-sibling::Adress,';')"/>
<xsl:for-each select="tokenize(.,';')">
<xsl:variable name="loc" select="position()"/>
<LINE>
<Name>
<xsl:value-of select="."/>
</Name>
<Address>
<xsl:value-of select="$addresses[$loc]"/>
</Address>
</LINE>
</xsl:for-each>
</xsl:template>

<xsl:template match="Adress"/>

</xsl:stylesheet>

///Peter
--
XML FAQ: http://xml.silmaril.ie/

0 new messages