help with XSLT

H

himgi

Hi to all I need to transform an xml document from a format to another.

In my source XML I have a tag value that is a list of codes. I give u
an example:
<mytag>1,2,33,44,55,6,7,88,99,...,nn</mytag>

Now I need to have this tag transformed as follow:
<MyNewTags>
<MyNewTag>1</MyNewTag>
<MyNewTag>2</MyNewTag>
<MyNewTag>33</MyNewTag>
<MyNewTag>44</MyNewTag>
<MyNewTag>55</MyNewTag>
<MyNewTag>6/MyNewTag>
<MyNewTag>7</MyNewTag>
<MyNewTag>88</MyNewTag>
<MyNewTag>99</MyNewTag>
....
<MyNewTag>nn</MyNewTag>
</MyNewTags>

How do i do that with XSLT?
Or when can I lookup to find the solution?

I did not find anything on the W3C Tutorial about XSLT..

Thanks in advance, Himgi.
 
A

Andy Dingley

himgi said:
In my source XML I have a tag value that is a list of codes. I give u
an example:
<mytag>1,2,33,44,55,6,7,88,99,...,nn</mytag>

This is an awkward problem in XSLT (not hard, just awkward). Your
input document is more of a text document than an XML document, because
the "interesting" part of the parsing is about parsing a text string,
not XML.

Here's a crude recursive solution.

<xsl:template name="foo" >
<xsl:param name="list" select="/.." />
<xsl:variable name="car" select="substring-before($list,',')" />
<xsl:variable name="cdr" select="substring-after($list,',')" />
<xsl:choose><xsl:when test="$car" >
<MyNewTag><xsl:value-of select="($car)" /></MyNewTag>
<xsl:call-template name="foo" >
<xsl:with-param name="list" select="($cdr)" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$list" ><MyNewTag><xsl:value-of select="$list"
/></MyNewTag></xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="mytag" >
<MyNewTags>
<xsl:call-template name="foo" >
<xsl:with-param name="list" select="text()" />
</xsl:call-template>
</MyNewTags>
</xsl:template>
 
H

himgi

Andy Dingley ha scritto:
This is an awkward problem in XSLT (not hard, just awkward). Your
input document is more of a text document than an XML document, because
the "interesting" part of the parsing is about parsing a text string,
not XML.

Here's a crude recursive solution.

<xsl:template name="foo" >
<xsl:param name="list" select="/.." />
<xsl:variable name="car" select="substring-before($list,',')" />
<xsl:variable name="cdr" select="substring-after($list,',')" />
<xsl:choose><xsl:when test="$car" >
<MyNewTag><xsl:value-of select="($car)" /></MyNewTag>
<xsl:call-template name="foo" >
<xsl:with-param name="list" select="($cdr)" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$list" ><MyNewTag><xsl:value-of select="$list"
/></MyNewTag></xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="mytag" >
<MyNewTags>
<xsl:call-template name="foo" >
<xsl:with-param name="list" select="text()" />
</xsl:call-template>
</MyNewTags>
</xsl:template>

Thank you very much. I really appreciate!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top