HandsOn Creating a Stylesheet to Convert Attribute Centric XML to Element Centric XML

1. Open Notepad and type the stylesheet code as shown below.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rs="urn:schemas-rrricrosoft-com:rowset"> <xsl:output method="xml" encoding="UTF-8" />

<xsl:template match="/">

<!-- root element for the XML output --> <Products xmlns:z="#RowsetSchema">

<xsl:for-each select="/xml/rs:data/z:row"> <Product>

<xsl:value-of select="."/> </xsl:element> </xsl:for-each> </Product> </xsl:for-each> </Products>

</xsl:template> </xsl:stylesheet>

2. Save the above stylesheet as AttribToElem.xsl in the Learn_XML folder. Make sure to type the .xsl extension so the file is not saved as text. We will use this stylesheet for the transformation in the next hands-on exercise.

Notice that in the above stylesheet, the @* wild card matches all attribute nodes. Each time the <z:row> tag is encountered, an element named <Product> will be created. And for each attribute, the attribute name will be converted to the element name using the built-in XPath Name( ) function. Expressions in curly braces are evaluated and converted to strings. The select-'." returns the current value of the attribute being read.

0 0

Post a comment