RE: [xsl] Replace values based on external files

Subject: RE: [xsl] Replace values based on external files
From: <Jarno.Elovirta@xxxxxxxxx>
Date: Fri, 14 Jan 2005 12:18:26 +0200
Hi,

> I need to implement a XSL that will replace text values in file1.xml
> based on replacement information in file2.xml.
>
> file2.xml provides an XPath that points to an attribute /
> element value
> and a replacement value. The XML has the form of:
>
> <mapping>
>     <!-- should replace @attribute1 value that are 'foo' of
> all element1
> elements in file1.xml with 'bar'-->
>     <replace>
>         <xpath>//element1/@attribute1[.='foo']</xpath>
>         <with>bar</with>
>     </replace>
>     <replace>
>         ...                    <!-- other replacement operations -->
>     </replace>
>     ...
> </mapping>
>
> Is there an easy way to dynamically generate replacement
> instructions in
> my XSL. I thought of extracting replacement information from file2.xml
> with the document function.

Either run file2.xml through

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:axsl="http://www.w3.org/1999/XSL/Transform/alias";>
  <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
  <xsl:template match="mapping">
    <axsl:stylesheet version="1.0">
      <xsl:for-each select="replace">
        <axsl:template match="{xpath}">
          <axsl:attribute name="{{name()}}">
            <xsl:value-of select="with"/>
          </axsl:attribute>
        </axsl:template>
      </xsl:for-each>
      <axsl:template match="@* | node()">
        <axsl:copy>
          <axsl:apply-templates select="@* | node()"/>
        </axsl:copy>
      </axsl:template>
    </axsl:stylesheet>
  </xsl:template>
</xsl:stylesheet>

and then process file1.xml with the generated stylesheet. Or use Mike's
solution that used the extension function.

Cheers,

Jarno

Current Thread