Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: multi step transformation >Thread Next - Re: multi step transformation Re: multi step transformationTo: NULL Date: 10/2/2004 12:30:00 AM "Marrow" <m--a-r-r-o-w@m-a-r-r-o-w--s-o-f-t.com> wrote in message news:<ef#dVTAqEHA.2724@T...>...
> Hi,
>
> Staring with some well-formed XML like...
>
> <?xml version="1.0"?>
> <item>
> <a>tom
> dick
> harry
> peter</a>
> <b>typeforA</b>
> </item>
>
>
> Something like...
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:template match="/">
> <output>
> <xsl:apply-templates select="item"/>
> </output>
> </xsl:template>
>
> <xsl:template match="item">
> <xsl:call-template name="splitA">
> <xsl:with-param name="text" select="string(a)"/>
> <xsl:with-param name="type" select="b"/>
> </xsl:call-template>
> </xsl:template>
>
> <xsl:template name="splitA">
> <xsl:param name="text"/>
> <xsl:param name="type"/>
> <xsl:choose>
> <xsl:when test="contains($text,' ')">
> <a b="{$type}">
> <xsl:value-of
> select="normalize-space(substring-before($text,' '))"/>
> </a>
> <xsl:call-template name="splitA">
> <xsl:with-param name="text"
> select="substring-after($text,' ')"/>
> <xsl:with-param name="type" select="$type"/>
> </xsl:call-template>
> </xsl:when>
> <xsl:when test="normalize-space($text)">
> <a b="{$type}">
> <xsl:value-of select="normalize-space($text)"/>
> </a>
> </xsl:when>
> </xsl:choose>
>
> </xsl:template>
> </xsl:stylesheet>
>
>
> HTH
> Marrow
> http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
> http://www.topxml.com/Xselerator
>
>
> "wooks" <wookiz@h...> wrote in message
> news:8852d33c.0410010840.2adad030@p......
> > have
> >
> > <a>tom
> > dick
> > harry
> > peter</a>
> > <b>typeforA</b>
> >
> > I want
> > <a b="typeforA>tom</a>
> > <a b="typeforA>dick</a>
> > <a b="typeforA>harry</a>
> > <a b="typeforA>peter</a>
> >
> > I have a generic split routine that works but to get the output I want
> > b has to be an attribute of a before the split routine runs. The split
> > routine will then add b as an attribute to each A it creates.
> >
> > Obviously I could do this easily in a 2 step transform -
> >
> > 1. transform b from an element to an attribute of A.
> > 2. Run the split.
> >
> > But I want to do it in one transform. How?
Memo to self - when asking for help be more explicit.
Below is my generic split routine (the input parameter is probably
otiose as it is just the text value of the element passed). It passes
the element to be split as well as the delimiter to split by. I want
each element created to have a copy of all the attributes that the
unsplit element had (as per the comments the xsl:for-each to handle
this is as yet untested).
The difficulty is that the input XML doesn't have any attribute nodes.
In this particular instance it is in element b, but the split routine
will no longer be generic if I pass it specific elements to be
converted to attributes (and there could be more than one element that
needs to be so converted).
So the constraints are -
1. Using the generic splitter which can be amended but not in a way as
to lose genericity.
2. Avoid a 2 step transformation.
The alternative is to accept that a generic routine cannot do this in
one pass, but something in my brain tells me XSLT should be capable of
better than that, I don't know how though.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template name="split">
<xsl:param name="elem"/>
<xsl:param name="input"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<!-- See if the input contains the search string -->
<xsl:when test="contains($input, $delimiter)">
<xsl:element name="{name($elem)}"
namespace="{namespace-uri($elem)}">
<xsl:value-of select="normalize-space(substring-before($input,
$delimiter))"/>
<!-- Add all the attributes this bit not tested-->
<xsl:for-each select="$elem/@*">
<xsl:attribute name="name(current())">
<xsl:value-of select="current()"/>
</xsl:attribute>
</xsl:for-each>
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="elem" select="$elem"/>
<xsl:with-param name="input" select="substring-after($input,
$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{name($elem)}"
namespace="{namespace-uri($elem)}">
<xsl:value-of select="$input"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
