我的错误,我说清楚:
我有一个简单的问题,但我是 XSLT 的新手:
我有 2 个 xml 文件:
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>
我想使用 XSLT 将 City Sunnyvale 和 San Jose 的所有值和属性替换为usa1.xml
来自usa2.xml
.
想法输出为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>
我怎样才能做到这一点?
我尝试了以下 XSLT,但返回的输出不是我所期望的:
<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>