Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 1st, 2006, 04:05 PM
himgi
Guest
 
Posts: n/a
Default help with XSLT

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.

  #2  
Old August 1st, 2006, 05:25 PM
Andy Dingley
Guest
 
Posts: n/a
Default Re: help with XSLT

himgi wrote:
Quote:
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>

  #3  
Old August 2nd, 2006, 09:45 AM
himgi
Guest
 
Posts: n/a
Default Re: help with XSLT


Andy Dingley ha scritto:
Quote:
himgi wrote:
>
Quote:
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>
Thank you very much. I really appreciate!

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles