xml - XSLT newbie, replacing values -
my mistake, make clear:
i have simple question, new xslt:
i have 2 xml files:
usa1.xml
:
<?xml version="1.0" encoding="utf-8"?> <country> <state name="ca"> <city name="sunnyvale" county="sant clara"> <street number="123"> el comino ave. </street> </city> <city name="san jose" county="sant clara"> <street number="345"> de anza ave. </street> </city> <city name="palo alto" county="sant clara"> <street number="789"> shoreline ave. </street> </city> </state> </country>
usa2.xml
:
<?xml version="1.0" encoding="utf-8"?> <country> <state name="ca"> <city name="sunnyvale" county="sant clara"> <street number="999"> homestead ave. </street> </city> <city name="san jose" county="sant clara"> <street number="888"> airport ave. </street> </city> </state> </country>
i want use xslt replace values , attributes of city sunnyvale , san jose in usa1.xml
data usa2.xml
.
idea output usa4.xml:
usa4.xml
:
<?xml version="1.0" encoding="utf-8"?> <country> <state name="ca"> <city name="sunnyvale" county="sant clara"> <street number="999"> homestead ave. </street> </city> <city name="san jose" county="sant clara"> <street number="888"> airport ave. </street> </city> <city name="palo alto" county="sant clara"> <street number="789"> shoreline ave. </street> </city> </state> </country>
how can this?
i tried following xslt, returned output not expecting:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <xsl:param name="usaxml" select="'usa1.xml'" /> <xsl:variable name="address" select="document($usaxml)//" /> <xsl:template match="/"> <xsl:attribute name="number"> <xsl:value-of select="$address/@street" /> </xsl:attribute> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
i use key cross reference between 2 files admittedly code becomes bit convoluted xslt 1.0 if need use keys between documents:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <xsl:key name="k1" match="state/city" use="concat(../@name, '|', @name)"/> <xsl:param name="usaxml" select="'test2013050302.xml'" /> <xsl:variable name="address" select="document($usaxml)" /> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="state/city"> <xsl:copy> <xsl:variable name="this" select="."/> <xsl:for-each select="$address"> <xsl:variable name="ref-data" select="key('k1', concat($this/../@name, '|', $this/@name))"/> <xsl:choose> <xsl:when test="$ref-data"> <xsl:apply-templates select="$ref-data/@* | $ref-data/node()"/> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="$this/@* | $this/node()"/> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:copy> </xsl:template> </xsl:stylesheet>
so primary input being
<?xml version="1.0" encoding="utf-8"?> <country> <state name="ca"> <city name="sunnyvale" county="sant clara"> <street number="123"> el comino ave. </street> </city> <city name="san jose" county="sant clara"> <street number="345"> de anza ave. </street> </city> <city name="palo alto" county="sant clara"> <street number="789"> shoreline ave. </street> </city> </state> </country>
and parameter file test2013050302.xml
being
<?xml version="1.0" encoding="utf-8"?> <country> <state name="ca"> <city name="sunnyvale" county="sant clara"> <street number="999"> homestead ave. </street> </city> <city name="san jose" county="sant clara"> <street number="888"> airport ave. </street> </city> </state> </country>
the transformation result is
<country> <state name="ca"> <city name="sunnyvale" county="sant clara"> <street number="999"> homestead ave. </street> </city> <city name="san jose" county="sant clara"> <street number="888"> airport ave. </street> </city> <city name="palo alto" county="sant clara"> <street number="789"> shoreline ave. </street> </city> </state> </country>
using xslt 2.0 processor , xslt 2.0 vastly simplify template
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <xsl:key name="k1" match="state/city" use="concat(../@name, '|', @name)"/> <xsl:param name="usaxml" select="'test2013050302.xml'" /> <xsl:variable name="address" select="document($usaxml)" /> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="state/city"> <xsl:copy> <xsl:variable name="ref-data" select="key('k1', concat(../@name, '|', @name), $address)"/> <xsl:choose> <xsl:when test="$ref-data"> <xsl:apply-templates select="$ref-data/(@*, node())"/> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="@* , node()"/> </xsl:otherwise> </xsl:choose> </xsl:copy> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment