Connecting Tech Pros Worldwide Forums | Help | Site Map

XSL replacing tags from xhtml to xhtml

Member
 
Join Date: Apr 2008
Location: Illinois
Posts: 92
#1: Aug 27 '08
I have xsl code which I was hoping could be used to replace one specific tag from an xhtml document and output another xhtml document.

xsl has phenomenal potential in data replacing, but coming from C/C++ background, it looks like a functional language I'm not familiar with.

Here's the link to where I got the code (thanks goes to the author), but I don't have a strong enough background in xsl to tell what is going on.

xslfile.xsl
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2.  
  3. <xsl:stylesheet version="2.0"
  4.   xmlns:xhtml="http://www.w3.org/1999/xhtml"
  5.   xmlns="http://www.w3.org/1999/xhtml"
  6.   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  7.   xmlns:xs="http://www.w3.org/2001/XMLSchema"
  8.   exclude-result-prefixes="xhtml xsl xs">
  9.  
  10. <xsl:output method="xml" version="1.0" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" indent="yes"/> 
  11.  
  12. <!-- the identity template -->
  13. <xsl:template match="@*|node()">
  14.   <xsl:copy>
  15.     <xsl:apply-templates select="@*|node()"/>
  16.   </xsl:copy>
  17. </xsl:template>
  18.  
  19. <!-- template for the head section. Only needed if we want to change, delete or add nodes. In our case we need it to add a link element pointing to an external CSS stylesheet. -->
  20.  
  21. <xsl:template match="xhtml:head">
  22.   <xsl:copy>
  23.     <link rel="StyleSheet" href="xhtml_test.css" type="text/css"/>
  24.     <xsl:apply-templates select="@*|node()"/>
  25.   </xsl:copy>
  26. </xsl:template>
  27.  
  28. <!-- template for the body section. Only needed if we want to change, delete or add nodes. In our case we need it to add a div element containing a menu of navigation. -->
  29.  
  30. <xsl:template match="xhtml:body">
  31.   <xsl:copy>
  32.  
  33.     <!-- Where I want the code to find all occurrences of
  34.           <span> </span> within the body and replace it with
  35.           <div> </div> (as an example--maybe with attributes included?) -->
  36.  
  37.     <xsl:apply-templates select="@*|node()"/>
  38.   </xsl:copy>
  39. </xsl:template>
  40. </xsl:stylesheet>
  41.  
xslfile.xhtml
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2.  
  3. <?xml-stylesheet type="text/xsl" href="transform.xsl"?> <!-- To view xml in foxfire -->
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  7.   <body>
  8.  
  9.   <span width="20%" id="name"> stuff </div>
  10.  
  11.   <!-- rest of html goes here -->
  12.   </body>
  13. </html>
  14.  
I know if I could understand how xsl files are parsed I know I could figure out how xsl works, but to me right now, that's a black box. <xsl:template > seems like a function which <xsl:apply-templates > calls, but tell me if I'm wrong on this.

I would like to figure this out without help, but being stuck learning this has been frustrating. Any help would be greatly appreciated.

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#2: Aug 27 '08

re: XSL replacing tags from xhtml to xhtml


Quote:

Originally Posted by TamusJRoyce

I know if I could understand how xsl files are parsed I know I could figure out how xsl works, but to me right now, that's a black box. <xsl:template > seems like a function which <xsl:apply-templates > calls, but tell me if I'm wrong on this.

<xsl:template> indeed is similar to a function.
I recommend reading a XSL tutorial like http://www.w3schools.com/xsl/ and http://www.w3schools.com/xpath/ (you need XPath for accessing the nodes of your xml file you need to process) for a start.
Reply