XML : xslt Converting text nodes to attributes

I have made an xslt script that will take any xml script and convert the text nodes within the elements to attributes of those elements. The elements will only have child elements or text nodes but wouldnt have attributes or anything else e.g. 12 should end up looking like:

here is my script it basically has two ifs saying if the element has a text node make a new element with the same name but make the text an attribute otherwise if it doesnt just copy the element

  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL  /Transform">  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>  <xsl:strip-space elements="*"/>  <xsl:template match="/">           <xsl:for-each select="*">                  <xsl:when test="./text()"/>                             <xsl:element name="{local-name()}" >                          <xsl:attribute name="Val">                                <xsl:value-of select="normalize- space(text())"/>                           </xsl:attribute>                      </xsl:element>                      </xsl:when>                      <xsl:otherwise>                               <xsl:copy-of select="."/>                     </xsl:otherwise>                        </xsl:for-each>      </xsl:template>    

No comments:

Post a Comment