user2161668
user2161668

Reputation: 23

XSLT: Simulating a template match inside a variable

I would like to know how can I simulate a template mach inside a variable based on the code below.

The output of this XSLT code I would like to store it in a variable and use it later in the script:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="*">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

I can achieve this now by using 2 XSLT scripts but I would like only to use one script.

The XSLT script should give the same output like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="InputNew">
  <xsl:template match="*">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
  </xsl:variable>

  <xsl:template match="/">
     <xsl:copy-of select="$InputNew"/>
  </xsl:template>
</xsl:stylesheet>

I know I can't use a template match inside a variable, I can only use call-template but I am unable to do the right XSLT code for this.

EDIT: Here is the working script:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsl soap xsi">
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes" omit-xml-declaration="yes"/>
   <xsl:strip-space elements="*"/>

    <xsl:template match ="*" mode="stripNs">
        <xsl:element name ="{local-name()}" >
            <xsl:apply-templates select ="@* | node()" mode="stripNs"/>
        </xsl:element>
    </xsl:template>


    <xsl:variable name="noNamespaces">
       <xsl:apply-templates select="soap:Envelope/soap:Body/*" mode="stripNs" />
    </xsl:variable>

    <xsl:template match="/">
       <!-- Now I can do what I want with the variable $noNamespaces -->
       <xsl:copy-of select="$noNamespaces"/>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 2

Views: 1553

Answers (1)

JLRishe
JLRishe

Reputation: 101748

If you're using Saxon, you should be able to use the exslt:node-set() function to do this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:exslt="http://exslt.org/common">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="*" mode="stripNs">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()" mode="stripNs"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*" mode="stripNs">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="/">
    <!-- Apply the stripNs templates and capture the result in a variable -->
    <xsl:variable name="noNamespaces">
      <xsl:apply-templates select="*" mode="stripNs" />
    </xsl:variable>

    <!-- Now use the variable -->
    <xsl:apply-templates select="exslt:node-set($noNamespaces)/*" />
  </xsl:template>
</xsl:stylesheet>

Then again, if you're using Saxon, you could also use XSLT 2.0 and use the same as the above, but without needing to use exslt:node-set() around $noNamespaces:

<!-- Now use the variable -->
<xsl:apply-templates select="$noNamespaces/*" />

Upvotes: 1

Related Questions