I. 这个 XSLT 1.0 转换(相应的 XSLT 2.0 解决方案更短更容易):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kAddrById" match="Address"
use="concat(../../CustId, '+', ../../CustName,
'+', AddressesId)"/>
<xsl:key name="kNodeByGenId" match="node()"
use="generate-id()"/>
<xsl:param name="pUpdatesPath" select=
"'file:///c:/temp/delete/CustomersUpdates.xml'"/>
<xsl:variable name="vUpdates" select=
"document($pUpdatesPath)"/>
<xsl:variable name="vmainDoc" select="/"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Addresses">
<Addresses>
<xsl:apply-templates select="Address | $vUpdates/*/*/*">
<xsl:sort select="AddressesId" data-type="number"/>
</xsl:apply-templates>
</Addresses>
</xsl:template>
<xsl:template match="Address">
<xsl:variable name="vIsThisUpdate" select=
"generate-id(/) = generate-id($vUpdates)"/>
<xsl:variable name="vOtherDoc" select=
"$vmainDoc[$vIsThisUpdate]
|
$vUpdates[not($vIsThisUpdate)]
"/>
<xsl:variable name="vCustId" select="../../CustId"/>
<xsl:variable name="vCustName" select="../../CustName"/>
<xsl:variable name="vAddrId" select="AddressesId"/>
<xsl:variable name="vOtherNodeId">
<xsl:for-each select="$vOtherDoc">
<xsl:value-of select=
"generate-id(key('kAddrById',
concat($vCustId,'+', $vCustName,
'+', $vAddrId)
)
)"/>
</xsl:for-each>
</xsl:variable>
<xsl:apply-templates mode="selected"
select="self::node()
[$vIsThisUpdate
or
(not($vIsThisUpdate) and not(string($vOtherNodeId)))
]">
<xsl:with-param name="pIsUpdating" select="$vIsThisUpdate"/>
<xsl:with-param name="pOtherDoc" select="$vOtherDoc"/>
<xsl:with-param name="pOtherNodeId"
select="string($vOtherNodeId)"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Address" mode="selected">
<xsl:param name="pIsUpdating"/>
<xsl:param name="pOtherDoc" select="/.."/>
<xsl:param name="pOtherNodeId"/>
<xsl:variable name="vStatus">
<xsl:choose>
<xsl:when test="$pIsUpdating and not($pOtherNodeId)">New</xsl:when>
<xsl:when test="$pIsUpdating">
<xsl:variable name="vOldStreet">
<xsl:for-each select="$pOtherDoc">
<xsl:value-of select=
"key('kNodeByGenId', $pOtherNodeId)/Street"/>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test=
"Street = string($vOldStreet)">NoChange</xsl:when>
<xsl:otherwise>Updated</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>Deleted</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<Address>
<Address status="{$vStatus}"/>
<xsl:apply-templates/>
</Address>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<Customer>
<CustId>1</CustId>
<CustName>Acme</CustName>
<Addresses>
<Address>
<AddressesId>1</AddressesId>
<Street>123 Main</Street>
</Address>
<Address>
<AddressesId>2</AddressesId>
<Street>345 Main</Street>
</Address>
<Address>
<AddressesId>4</AddressesId>
<Street>888 Goner St.</Street>
</Address>
</Addresses>
</Customer>
并在c:/temp/delete/CustomersUpdates.xml
此 XML 文档中(与提供的略有不同,以使第一个地址以状态“NoChange”结束):
<Customer>
<CustId>1</CustId>
<CustName>Acme</CustName>
<Addresses>
<Address>
<AddressesId>1</AddressesId>
<Street>123 Main</Street>
</Address>
<Address>
<AddressesId>2</AddressesId>
<Street>999 Updated St.</Street>
</Address>
<Address>
<AddressesId>3</AddressesId>
<Street>3999 New St.</Street>
</Address>
</Addresses>
</Customer>
产生想要的正确结果:
<Customer>
<CustId>1</CustId>
<CustName>Acme</CustName>
<Addresses>
<Address>
<Address status="NoChange"/>
<AddressesId>1</AddressesId>
<Street>123 Main</Street>
</Address>
<Address>
<Address status="Updated"/>
<AddressesId>2</AddressesId>
<Street>999 Updated St.</Street>
</Address>
<Address>
<Address status="New"/>
<AddressesId>3</AddressesId>
<Street>3999 New St.</Street>
</Address>
<Address>
<Address status="Deleted"/>
<AddressesId>4</AddressesId>
<Street>888 Goner St.</Street>
</Address>
</Addresses>
</Customer>
二、XSLT 2.0 解决方案:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kAddrById" match="Address"
use="concat(../../CustId, '+', ../../CustName,
'+', AddressesId)"/>
<xsl:param name="pUpdatesPath" select=
"'file:///c:/temp/delete/CustomersUpdates.xml'"/>
<xsl:variable name="vUpdates" select=
"document($pUpdatesPath)"/>
<xsl:variable name="vmainDoc" select="/"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Addresses">
<Addresses>
<xsl:apply-templates select="Address | $vUpdates/*/*/*">
<xsl:sort select="AddressesId" data-type="number"/>
</xsl:apply-templates>
</Addresses>
</xsl:template>
<xsl:template match="Address[root() is $vUpdates]">
<xsl:variable name="vOtherDoc" select="$vmainDoc"/>
<xsl:variable name="vOtherNode" select="my:OtherNode(., $vOtherDoc)"/>
<xsl:variable name="vStatus" select=
"concat('New'[not($vOtherNode)],
'NoChange'[$vOtherNode
and current()/Street eq $vOtherNode/Street],
'Updated'[$vOtherNode and current()/Street ne $vOtherNode/Street]
)"/>
<xsl:apply-templates select="self::node()" mode="selected">
<xsl:with-param name="pStatus" select="$vStatus"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Address[not(root() is $vUpdates)]">
<xsl:variable name="vOtherDoc" select="$vUpdates"/>
<xsl:variable name="vOtherNode" select="my:OtherNode(., $vOtherDoc)"/>
<xsl:apply-templates select="self::node()[not($vOtherNode)]" mode="selected">
<xsl:with-param name="pStatus" select="'Deleted'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Address" mode="selected">
<xsl:param name="pStatus"/>
<Address>
<Address status="{$pStatus}"/>
<xsl:apply-templates/>
</Address>
</xsl:template>
<xsl:function name="my:OtherNode" as="element()?">
<xsl:param name="pThis" as="element()"/>
<xsl:param name="pOtherDoc" as="document-node()"/>
<xsl:sequence select=
"key('kAddrById',
concat($pThis/../../CustId,'+', $pThis/../../CustName,
'+', $pThis/AddressesId
),
$pOtherDoc
)"/>
</xsl:function>
</xsl:stylesheet>
当此转换应用于相同的文档时,会产生相同的正确结果。