How to convert element attribute to element name in XSLT?

I would bind to convert the XML with attributes, such as the name attribute, in the following:

<books>
  <book name="TheBumperBookOfXMLProgramming"/>
  <book name="XsltForDummies"/>
</books>

into elements called a name attribute:

<books>
  <TheBumperBookOfXMLProgramming/>
  <XsltForDummies/>
</books>

using XSLT. Any ideas?

+3
source share
2 answers

You can create items by name using xsl:element:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
     version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <books>
      <xsl:apply-templates />
    </books>

  </xsl:template>

  <xsl:template match="book">
    <xsl:element name="{@name}" />
  </xsl:template>

</xsl:stylesheet>
+4
source
<xsl:template match="book">
   <xsl:element name="{@name}">
       <xsl:copy-of select="@*[name()!='name'] />
   </xsl:element>
</xsl:template>

it also copies any properties to an <book>unnamed 'name'

<book name="XsltForDummies" id="12" />

will turn into

<XsltForDummies id="12 />
+3
source

Source: https://habr.com/ru/post/1705578/


All Articles