2

I've created some markup/css and JavaScript for a custom UI to be used with the HTML5 <video> tag and would like to use XSLT to replace the video elements in my web pages with it.

Here is an example XHTML document that would be transformed:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="layout.css" type="text/css"?>
<?xml-stylesheet href="video_extension.xsl" type="text/xsl"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <title>Example</title>
  </head>
  <body>
    <video src="myvideo.webm"/>
  </body>
</html>

video_extension.xsl is the XSLT document I'm trying to create, and it's output should hopefully be this:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="layout.css" type="text/css"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <title>Example</title>
    <script src="video_extension.js" type="text/javascript"/>
    <link rel="stylesheet" href="video_extension.css" type="text/css"/>
  </head>
  <body>
    <div class="video-container">
      <video src="myvideo.webm"/>
      <div class="video-ui>
        <!-- additional markup not included -->
      </div>
    </div>
  </body>
</html>

It should leave the rest of the document as is, but add the video extension's CSS and JavaScript files and then wrap the video elements in a div along with the rest of my UI markup. This needs to work for any valid XHTML5 document and the output should also be valid XHTML5.

Thanks for any help.

Chris_F
  • 4,991
  • 5
  • 33
  • 63

1 Answers1

2

You can use the identity rule and override the wanted elements. For example, the following transform:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:x="http://www.w3.org/1999/xhtml">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="x:head">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <script src="video_extension.js" type="text/javascript" 
                xmlns="http://www.w3.org/1999/xhtml"/>
            <link rel="stylesheet" href="video_extension.css" type="text/css" 
                xmlns="http://www.w3.org/1999/xhtml"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="x:video">
        <div class="video-container" 
            xmlns="http://www.w3.org/1999/xhtml">
            <xsl:copy-of select="."/>
            <div class="video-ui">
                <!-- additional markup not removed -->
            </div>
        </div>
    </xsl:template>

</xsl:stylesheet>

outputs:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="layout.css" type="text/css"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
   <head>
      <title>Example</title>
      <script src="video_extension.js" type="text/javascript"/>
      <link rel="stylesheet" href="video_extension.css" type="text/css"/>
   </head>
   <body>
      <div class="video-container">
         <video src="myvideo.webm"/>
         <div class="video-ui"/>
      </div>
   </body>
</html>
Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67
  • That's great. I noticed that all the elements added by to the document have an xmlns attribute and XHTML namespace. Is there any way to prevent it from adding the xmlns attribute, since it is unnecessary? – Chris_F Sep 03 '11 at 21:37
  • Oh, just noticed you have the xmlns attributes defined in the XSLT itself. – Chris_F Sep 03 '11 at 21:43
  • Yes they are in the XSLT so that they willl not appear in the output :D – Emiliano Poggi Sep 03 '11 at 21:46
  • 1
    This is because, for literal elements if you tell the processor that the element is in the default namespace, it will not need to declare it in the output document. – Emiliano Poggi Sep 03 '11 at 21:47
  • 1
    So, @Chris, You got a good answer. Why haven't you accepted it yet? – Dimitre Novatchev Sep 04 '11 at 23:56