|
Subject: [xsl] output modified branch - best practice From: "Heiko Niemann" <kontakt@xxxxxxxxxxxxxxxx> Date: Sun, 20 Jun 2010 16:42:52 +0200 |
Hello,
the sample I will post here is working the way I want. My question will be
about best practices. This arose when I was searching a solution for this
problem.
source:
<root>
<base>
<data>
<first>John</first>
<last>Doe</last>
<dob/>
<zip>90210</zip>
<phone/>
<mobile>(999) 484 6584</mobile>
<email>john@xxxxxxxxx</email>
</data>
</base>
<update>
<data>
<dob>1977-12-20</dob>
<phone>(555) 229 8811</phone>
<mobile/>
<email>john@xxxxxxxxxx</email>
</data>
</update>
</root>
xslt:
<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@xxxxxxxxxx</email>
</data>
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.
So, is the easy solution a good solution? Is there a best practice to
output a modified branch?
Thanks for your help and suggestions,
Heiko
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| [xsl] XSL ver. 2 to copy, replace, , Henry Dong | Thread | Re: [xsl] output modified branch - , Martin Honnen |
| [xsl] Re: filtering problem, Adrian Herscu | Date | Re: [xsl] output modified branch - , Martin Honnen |
| Month |