Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Coverting the Elements to Attributes, of the an XML file - giving another XML file >Thread Next - Re: Coverting the Elements to Attributes, of the an XML file - giving another XML file Re: Coverting the Elements to Attributes, of the an XML file - giving another XML fileTo: NULL Date: 7/6/2006 6:41:00 PM
abrahamregisraj@g... wrote:
> <Level1>
^^^^^^^
> </level1>
^^^^^^
That is not well-formed, if the start tag has element name 'Level1' then
the end tag needs the same element name and not 'level1'.
And you even want that transformed:
> <Level1 name = "Sample 3 (level1)" description = "This is Sample
^^^^^^
> </level1>
^^^^^^^
That is not possible.
Assuming you have consistent naming of 'level<n>' then try this stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xmll" indent="yes" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(local-name(), 'level')]">
<xsl:copy>
<xsl:apply-templates select="name | description"
mode="element-to-attribute" />
<xsl:apply-templates select="*[not(self::name) and
not(self::description)]" />
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="element-to-attribute">
<xsl:attribute name="{name()}" namespace="{namespace-uri()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Note however that choosing element names where an index or a level shows
up in the element names is rather poor design.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
