我有以下xml:
car.xml:
<car ref-id="parts.xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<color>red</color>
<tire>michelin</tire>
<engines>
<engine>
<model>Z</model>
</engine>
</engines>
<hifi>pioneer</hifi>
</car>
parts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<parts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<engines>
<engine>
<model>X</model>
</engine>
<engine>
<model>Y</model>
</engine>
</engines>
<tire>goodyear</tire>
<color>black</color>
<airbag>true</airbag>
</parts>
我想合并parts.xml与car.xml,但只想从parts.xml中复制那些节点(不管它们的值),而这些节点在car.xml中不存在。
E。g,我需要以下输出:
<car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<color>red</color>
<tire>michelin</tire>
<engines>
<engine>
<model>Z</model>
</engine>
</engines>
<hifi>pioneer</hifi>
<airbag>true</airbag>
</car>
我被困在下面的转换合并了所有的元素:
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="loc">
<xsl:value-of select="car/@ref-id" />
</xsl:variable>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="car">
<xsl:copy>
<xsl:apply-templates select="@*|node() | document($loc)/parts/*" />
</xsl:copy>
</xsl:template>
</xsl:transform>
这是一个相当尴尬的安排,而解决方案(至少在XSLT 1.0中)也同样尴尬:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/car">
<xsl:variable name="local-names">
<xsl:for-each select="*">
<name><xsl:value-of select="name()"/></name>
</xsl:for-each>
</xsl:variable>
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:copy-of select="document(@ref-id)/parts/*[not(name()=exsl:node-set($local-names)/name)]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>