Reputation:
I need to change some of the element names in the original XML. I am trying to do this with XSLT, but can't get it to work.
Here is a sample of 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 did, 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!
Upvotes: 2
Views: 10368
Reputation: 6230
The following template achieves the result you're 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'd like more well formed markup modify 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"
/>
Upvotes: 2
Reputation: 161773
For anything like this, start with an identity transform:
<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 just copy every 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 pattern should get you what you want.
Upvotes: 10