在XSTL1.0中合并2个XML文件
我正在寻找一种合并2个XML文档的方法 -如果它们具有相同的属性 -从第一个文档中获取元素,但如果元素匹配,则从第二个文档中获取值 我可以从第一个或第二个元素中获取所有元素,但无法合并 这就是我尝试过的:在XSTL1.0中合并2个XML文件,xml,xslt,merge,xslt-1.0,Xml,Xslt,Merge,Xslt 1.0,我正在寻找一种合并2个XML文档的方法 -如果它们具有相同的属性 -从第一个文档中获取元素,但如果元素匹配,则从第二个文档中获取值 我可以从第一个或第二个元素中获取所有元素,但无法合并 这就是我尝试过的: <?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:
<?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>
真的
机器1
类型1
2
c:/somefolder/1
我
假的
假的
50
8.
1.
假的
自动的
32
256
256
真的
0
假的
机器2
类型2
2.1.1.7
d:/otherfolder
你
假的
假的
20000
5.
8.
假的
自动的
32
256
256
真的
5.
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>
假的
真的
7.
真的
习俗
别人
10
真的
64
1024
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>
真的
机器1
习俗
2
c:/somefolder/1
别人
假的
假的
50
8.
10
真的
自动的
64
1024
256
真的
0
假的
机器2
类型2
2.1.1.7
d:/otherfolder
你
真的
假的
20000
5.
8.
假的
自动的
32
256
256
真的
7.
你已经试过了吗?我试过这个,这个副本,或者第一个或第二个:请将它添加到你的帖子中,并确保它可用。。。()谢谢。完成了,我从更大的xsl中剥离了它(它仅用于绑定,下面是[另一个问题]):-)使绑定唯一,因此我的示例可以工作。