我正在寻找合并2个XML文档的方法 - 如果他们有相同的属性 - 从第一个文档中获取元素,但如果元素匹配,则从第二个文档中获取值。
我可以从第1个或第2个获得所有元素,但我无法合并。
这就是我尝试过的:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ds="http://www.test.com/xmlns/application" >
<xsl:param name="ADAPTER" select="'merge'"/>
<xsl:variable name="GLOBALS" select="document(concat($ADAPTER,'.xml'))"/>
<xsl:template match="*|/">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
<xsl:template match="//ds:binding">
<xsl:choose>
<xsl:when test="$GLOBALS//ds:binding[@name=current()/@name]">
<xsl:copy-of select="$GLOBALS//ds:binding[@name=current()/@name]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
这是input.xml的merge.xml和output.xml,以使我自己更清楚。
input.xml中
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://www.test.com/xmlns/application" name="test">
<services>
<service name="service1">
<enabled>true</enabled>
<bindings>
<binding name="binding1">
<machine>machine1</machine>
<product>
<type>type1</type>
<version>2.0</version>
<location>c:/somefolder/1</location>
</product>
<contact>me</contact>
<setting>
<startOnBoot>false</startOnBoot>
<enableVerbose>false</enableVerbose>
<maxLogFileSize>50</maxLogFileSize>
<maxLogFileCount>8</maxLogFileCount>
<threadCount>1</threadCount>
<NTService>
<runAsNT>false</runAsNT>
<startupType>automatic</startupType>
</NTService>
<java>
<initHeapSize>32</initHeapSize>
<maxHeapSize>256</maxHeapSize>
<threadStackSize>256</threadStackSize>
</java>
</setting>
<shutdown>
<checkpoint>true</checkpoint>
<timeout>0</timeout>
</shutdown>
</binding>
</bindings>
</service>
<service name="service2">
<enabled>false</enabled>
<bindings>
<binding name="binding2">
<machine>machine2</machine>
<product>
<type>type2</type>
<version>2.1.1.7</version>
<location>d:/otherfolder</location>
</product>
<contact>you</contact>
<setting>
<startOnBoot>false</startOnBoot>
<enableVerbose>false</enableVerbose>
<maxLogFileSize>20000</maxLogFileSize>
<maxLogFileCount>5</maxLogFileCount>
<threadCount>8</threadCount>
<NTService>
<runAsNT>false</runAsNT>
<startupType>automatic</startupType>
</NTService>
<java>
<initHeapSize>32</initHeapSize>
<maxHeapSize>256</maxHeapSize>
<threadStackSize>256</threadStackSize>
</java>
</setting>
<shutdown>
<checkpoint>true</checkpoint>
<timeout>5</timeout>
</shutdown>
</binding>
</bindings>
</service>
</services>
</application>
的merge.xml
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://www.test.com/xmlns/application" name="test">
<services>
<service name="service2">
<enabled>false</enabled>
<bindings>
<binding name="binding2">
<setting>
<startOnBoot>true</startOnBoot>
</setting>
<shutdown>
<timeout>7</timeout>
</shutdown>
</binding>
</bindings>
</service>
<service name="service1">
<enabled>true</enabled>
<bindings>
<binding name="binding1">
<product>
<type>custom</type>
</product>
<contact>someoneelse</contact>
<setting>
<threadCount>10</threadCount>
<NTService>
<runAsNT>true</runAsNT>
</NTService>
<java>
<initHeapSize>64</initHeapSize>
<maxHeapSize>1024</maxHeapSize>
</java>
</setting>
</binding>
</bindings>
</service>
</services>
</application>
的Output.xml
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://www.test.com/xmlns/application" name="test">
<services>
<service name="service1">
<enabled>true</enabled>
<bindings>
<binding name="binding1">
<machine>machine1</machine>
<product>
<type>custom</type>
<version>2.0</version>
<location>c:/somefolder/1</location>
</product>
<contact>someoneelse</contact>
<setting>
<startOnBoot>false</startOnBoot>
<enableVerbose>false</enableVerbose>
<maxLogFileSize>50</maxLogFileSize>
<maxLogFileCount>8</maxLogFileCount>
<threadCount>10</threadCount>
<NTService>
<runAsNT>true</runAsNT>
<startupType>automatic</startupType>
</NTService>
<java>
<initHeapSize>64</initHeapSize>
<maxHeapSize>1024</maxHeapSize>
<threadStackSize>256</threadStackSize>
</java>
</setting>
<shutdown>
<checkpoint>true</checkpoint>
<timeout>0</timeout>
</shutdown>
</binding>
</bindings>
</service>
<service name="service2">
<enabled>false</enabled>
<bindings>
<binding name="binding1">
<machine>machine2</machine>
<product>
<type>type2</type>
<version>2.1.1.7</version>
<location>d:/otherfolder</location>
</product>
<contact>you</contact>
<setting>
<startOnBoot>true</startOnBoot>
<enableVerbose>false</enableVerbose>
<maxLogFileSize>20000</maxLogFileSize>
<maxLogFileCount>5</maxLogFileCount>
<threadCount>8</threadCount>
<NTService>
<runAsNT>false</runAsNT>
<startupType>automatic</startupType>
</NTService>
<java>
<initHeapSize>32</initHeapSize>
<maxHeapSize>256</maxHeapSize>
<threadStackSize>256</threadStackSize>
</java>
</setting>
<shutdown>
<checkpoint>true</checkpoint>
<timeout>7</timeout>
</shutdown>
</binding>
</bindings>
</service>
</services>
</application>