<?xml version="1.0" encoding="iso-8859-1"?>

<!-- Transform <wikipedia> elements by giving a "name"
attribute. Typically used when an acronym gets expanded, for instance
<wikipedia>DNS</wikipedia> becomes <wikipedia name="Domain Name
System">DNS</wikipedia>. No longer in use (see the TODOs for why). See 
<http://www.bortzmeyer.org/transformation-texte.html> 

Use: 
* the XSLT parameter "old" is the former text of the <wikipedia> element
* the XSLT parameter "new" is the new text

For instance, with xsltproc:

% xsltproc - -stringparam old DNS - -stringparam new "Domain Name System" update-wp.xslt document.xml 

For ideas, see http://stackoverflow.com/questions/2052179/find-and-replace-text-in-xml-using-perl

-->

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

  <xsl:output method="xml" encoding="ISO-8859-1" 
    cdata-section-elements="code" /> <!-- You can have several element
                                     names here, space-separated -->

  <xsl:param name="old">No value for old</xsl:param>
  <xsl:param name="new">No value for new</xsl:param>

  <xsl:template match="wikipedia">
    <xsl:choose>
      <xsl:when test="text()=$old">
        <wikipedia><xsl:attribute name="name"><xsl:value-of select="$new"/></xsl:attribute><xsl:value-of select="$old"/></wikipedia>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy> 
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>        
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- TODO: numerical entities are not output with the base I prefer (16) -->
  <!-- TODO: entities defined in the XML declaration are replaced by their values Probably no solution :-( -->
  <xsl:template match="node()|@*">
    <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

