Renaming XML Elements Using XSLT

I need to change some element names in the source XML. I am trying to do this using XSLT, but cannot make it work.

Here is an example XML:

<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="test.xsl" type="text/xsl"?> <html> <body> <section>Jabber</section> <itemtitle>JabberJabber</itemtitle> <p>Always Jabber Jabber Jabber</p> <h3>Emboldened Requests </h3> <p>Somemore Jabber Here</p> <img scr="bigpicture.jpg"></img> <poll><p>Which statement best characterizes you?</p></poll> <pcredit>Left: Jumpin Jasper/Jumpy Images</pcredit> </body> </html> 

I need to change it to:

 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="test.xsl" type="text/xsl"?> <html> <body> <div class="issuehead">Jabber</div> <div class="issuetitle">JabberJabber</div> <p>Always Jabber Jabber Jabber</p> <h3>Emboldened Requests </h3> <p>Somemore Jabber Here</p> <img scr="bigpicture.jpg"></img> <div class="poll"><p>Which statement best characterizes you?</p></div> <div class="pcredit">Left: Jumpin Jasper/Jumpy Images</div> </body> </html> 

Here is the XSLT I made, but I can't get it to work:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/"> <html> <head> </head> <body> <xsl:apply-templates/> </body> </html> <xsl:template match="section"> <div class="issuehead"><xsl:value-of select="."/></div> </xsl:template> <xsl:template match="itemtitle"> <div class="issuetitle"> <xsl:value-of select="."/></div> </xsl:template> <xsl:template match="img"></xsl:template> <xsl:template match="poll"> <div class="poll"><xsl:value-of select="."/></div> </xsl:template> <xsl:template match="pcredit"> <div class="pcredit"><xsl:value-of select="."/></div> </xsl:template> <xsl:template match="p"></xsl:template> <xsl:template match="h3"></xsl:template> </xsl:template> </xsl:stylesheet> 

Thanks for the help!

+2
source share
2 answers

For something like this, start by transforming your identity:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="node( ) | @*"> <xsl:copy> <xsl:apply-templates select="@* | node( )"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 

This will simply copy each node. But then you add additional templates to do what you need:

 <xsl:template match="section"> <div class="issuehead"> <xsl:apply-templates select="@* | node( )"/> </div> </xsl:template> 

This template should get what you want.

+10
source

The following template achieves the result you are looking for:

 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <!-- Copy all elements and attributes from the source to the target --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <!-- Transform the given elements to divs with class names matching the source element names --> <xsl:template match="itemtitle|poll|pcredit"> <div class="{local-name(.)}"> <xsl:value-of select="."/> </div> </xsl:template> <!-- Transform the section element to a div of class 'issuehead' --> <xsl:template match="section"> <div class="issuehead"> <xsl:value-of select="."/> </div> </xsl:template> </xsl:stylesheet> 

If you want more well-formed markup to change the xsl: output tag as follows:

 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /> 
+2
source

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


All Articles