xsl-list
[Top] [All Lists]

Re: [xsl] output modified branch - best practice

2010-06-20 10:29:33
Heiko Niemann wrote:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">
  <xsl:output method="xml" encoding="UTF-8"/>
  <xsl:template match="/root/base/data">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="/root/base/data/*">
    <xsl:variable name="var.update" select="/root/update/data/*[name() =
current()/name()]"/>
    <xsl:choose>
      <xsl:when test="exists($var.update)">
        <xsl:copy-of select="$var.update"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="text()"/>
</xsl:stylesheet>


output:

<data>
  <first>John</first>
  <last>Doe</last>
  <dob>1977-12-20</dob>
  <zip>90210</zip>
  <phone>(555) 229 8811</phone>
  <mobile/>
  <email>john(_at_)google(_dot_)com</email>
</data>

Does that stylesheet you posted really output the above nicely indented document? I think because of the empty template for text() nodes you lose the indentation.


So base/data and update/data are merged. Each element in base/data is
copied unless the same element exists in update/data; then it will be
overwritten.

Since I just want the modified base/data branch as output I have to get
rid of whatever the built-in template rules produces. After some research
I found that <xsl:template match="text()"/> is the easiest solution. But
in one post the author said that this was not such a good idea.
Unfortunately I  did not bookmark it.

Your stylesheet relies on built-in templates to process the branch you are interested and then suppresses any output from other branches. You could instead write a template for the root node that ensures only the branch you are interested in is processed, then you don't need to suppress output from other branches.
And you could use a key to find the update stuff:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="k1" match="/root/update/data/*" use="node-name(.)"/>

  <xsl:template match="/">
    <xsl:apply-templates select="root/base/data"/>
  </xsl:template>

  <xsl:template match="/root/base/data">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/root/base/data/*">
    <xsl:variable name="var.update" select="key('k1', node-name(.))"/>
    <xsl:choose>
      <xsl:when test="exists($var.update)">
        <xsl:copy-of select="$var.update"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

--

        Martin Honnen
        http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>