XSL replacing tags from xhtml to xhtml

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TamusJRoyce
    New Member
    • Apr 2008
    • 108

    XSL replacing tags from xhtml to xhtml

    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
    Code:
    <?xml version="1.0"?>
    
    <xsl:stylesheet version="2.0"
      xmlns:xhtml="http://www.w3.org/1999/xhtml"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xhtml xsl xs">
    
    <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"/> 
    
    <!-- the identity template -->
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    
    <!-- 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. -->
    
    <xsl:template match="xhtml:head">
      <xsl:copy>
        <link rel="StyleSheet" href="xhtml_test.css" type="text/css"/>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    
    <!-- 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. -->
    
    <xsl:template match="xhtml:body">
      <xsl:copy>
    
        <!-- [I]Where I want the code to find all occurrences of
              <span> </span> within the body and replace it with
              <div> </div> (as an example--maybe with attributes included?)[/I] -->
    
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>
    xslfile.xhtml
    Code:
    <?xml version="1.0"?>
    
    <?xml-stylesheet type="text/xsl" href="transform.xsl"?> <!-- To view xml in foxfire -->
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <body>
    
      <span width="20%" id="name"> stuff </div>
    
      <!-- rest of html goes here -->
      </body>
    </html>
    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
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    Working...