Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Create node with outo name >Thread Next - Re: Create node with outo name Re: Create node with outo nameTo: NULL Date: 3/6/2005 9:12:00 AM "sg" <sg@d...> wrote in message
news:AA136C57-0D8F-45C3-BCEA-947094A2B31E@m......
> hi,
>
> I have this xml:
> <root>
> <book>
> <name>book1</name
> </book>
> <book>
> <name>book1</name
> </book>
> .
> .
> .
> </root>
>
> and I'm tring to transfer it to this:
> <root>
> <book1>
> <name>book1</name
> </book>
> <book2>
> <name>book1</name
> </book>
> .
> .
> <bookn>
> <name>book1</name
> </book>
>
> How can I do this using XSL?
I wholeheartedly recommend against this, it's very difficult to process this
type of markup, you can't easily sort or select all the book(n) elements.
This would be the stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates select="book"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<xsl:element name="{concat('book', position())}">
<xsl:copy-of select="name"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I'd prefer:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates select="book"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<book position="{position()}">
<xsl:copy-of select="name"/>
</book>
</xsl:template>
</xsl:stylesheet>
Which would give:
<root>
<book position="1">
<name>book1</name>
</book>
<book position="2">
<name>book1</name>
</book>
...
</root>
--
Joe (MVP - XML)
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
