Difference between revisions of "Copy-All.xsl"

From TEIWiki
Jump to navigation Jump to search
m (remove blank before XML declaration)
m
Line 55: Line 55:
  
 
</xsl:stylesheet>
 
</xsl:stylesheet>
 
 
</nowiki></pre>
 
</nowiki></pre>

Revision as of 02:48, 6 October 2006


Introduction

This is a utility XSLT script which copies the entire contents of an XML document to the output, except those elements specifically matched. One manner in which to use it is to include the statement:

 <xsl:import href="Copy-All.xsl"/>

at the beginning of your XSLT. (See the stylesheets which do this as an example.)

Use with "PipedStylesheets.bash" and "P4 enroute to P5.bash"

If using either of these bash scripts to pipeline a number of XSLT transformations, this stylesheet should be placed in the same directory as the script and the other stylesheets. (They import this one.) However, in naming this stylesheet make sure to call it 'Copy-All.xsl'. Specifically, make sure you have it capitalised, and that it ends '.xsl' not '.xslt' as the other stylesheets.

General Purpose Use

You can use this stylesheet as the basis of one to copy the entire contents of any XML document *except* do something to some particular elements. The fictional element fooBar is used as an example of how one would do something different to a particular element. However, if you are going to write several stylesheets, each using this functionality, then importing it in as above probably a better idea.

Stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- XSLT Template to copy anything, priority="-1" -->
  <xsl:template match="@*|node()|text()|comment()|processing-instruction()" priority="-1">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()"/>
    </xsl:copy>
  </xsl:template>

  <!-- If I were to do something different for an element here is how I'd do it. -->
  <xsl:template match="fooBar">
    <barFoo><xsl:apply-templates/></barFoo>
  </xsl:template>

</xsl:stylesheet>

P5 namespace version

If you are processing a TEI P5 document, whose elements will therefore be in the TEI namespace, and if you have an XSLT 2.0 processor, the following variant of the stylesheet makes your life easier by defining the xpath-default-namespace attribute on the stylesheet so that you can omit the tei: prefix in your XPath expressions:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
   xpath-default-namespace="http://www.tei-c.org/ns/1.0">

  <!-- XSLT Template to copy anything, priority="-1" -->
  <xsl:template match="@*|node()|text()|comment()|processing-instruction()" priority="-1">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()"/>
    </xsl:copy>
  </xsl:template>

  <!-- If I were to do something different for an element here is how I'd do it. -->
  <xsl:template match="fooBar">
    <barFoo><xsl:apply-templates/></barFoo>
  </xsl:template>

</xsl:stylesheet>