Question

This xsl:apply-templates instruction:

<xsl:if test="$item/tcm:Content/em:Office/em:Address">
    <address>
        <xsl:apply-templates mode="renderhtml" select="$item/tcm:Content/em:Office/em:Address/node()" />
    </address>
</xsl:if>

produces:

<p>
    My Office Address
    <br />XXXX Road
    <br />XXXX District
    <br />XXXX, XXXX.
    <br />PO Box XXXX
</p>

Now I want to store the produced html in a variable and later call another template to replace the first <p> element with <p property="v:street-address">, so that it the final result would be:

<p property="v:street-address">
    My Office Address
    <br />XXXX Road
    <br />XXXX District
    <br />XXXX, XXXX.
    <br />PO Box XXXX
</p>

Please suggest!!

Thanks

Was it helpful?

Solution

In XSLT 1.0, you can only capture an output and apply templates to that output is if you are using a processor that supports the node-set() function. Assuming that you are, you can do something like this:

<xsl:if test="$item/tcm:Content/em:Office/em:Address">
    <address>
      <xsl:variable name="addressHtml">
        <xsl:apply-templates mode="renderhtml" 
                             select="$item/tcm:Content/em:Office/em:Address/node()" />
      </xsl:variable>
      <xsl:apply-templates select="msxsl:node-set($addressHtml)" 
                           mode="adjustAddressHtml" />
    </address>
</xsl:if>

...

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

  <xsl:template match="/" mode="adjustAddressHtml">
    <xsl:if test="p">
      <xsl:call-template name="Identity" />
    </xsl:if>
    <xsl:if test="not(p)">
      <xsl:call-template name="StartingP" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="/p[1]" mode="adjustAddressHtml" name="StartingP">
    <p property="v:street-address">
      <xsl:apply-templates select="@* | node()" mode="adjustAddressHtml" />
    </p>
  </xsl:template>

OTHER TIPS

Without a source XML document provided it is difficult to give a complete answer, but in general one can avoid the need of a two-pass transformation.

Instead, the XSLT code can be somethong as simple as:

Somewhere:

<xsl:apply-templates mode="renderhtml"
         select="$item/tcm:Content/em:Office/em:Address/>

Then:

 <xsl:template match="em:Address" mode="renderhtml">
  <address>
      <xsl:apply-templates mode="renderhtml" select="node()" />
  </address>
 </xsl:template>

 <xsl:template match="em:Address/node()" mode="renderhtml">
    <p>
     <xsl:if test="position() = 1">
      <xsl:attribute name="property">v:street-address</xsl:attribute>
     </xsl:if>
       <!-- The code that generates this:
        My Office Address
        <br />XXXX Road
        <br />XXXX District
        <br />XXXX, XXXX.
        <br />PO Box XXXX
         -->
    </p>
 </xsl:template>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top