我正在寻求使用XSLT1.0替换两个XML文件中的值的帮助。我相信我已经接近了,但需要一些指导来微调我迄今为止的成绩。
首先是问题:这两个XML文件本质上是相同的。我将第一个文件中的两个节点替换为第二个文件及其所有子文件中的节点,但我当前的实现正在创建重复节点。我确信这是因为我的XPath返回了我给它的所有节点,但我不知道如何调整谓词,以便它只在匹配兄弟节点时进行替换,特别是。
文件1-Networks.xml
<?xml version="1.0" encoding="utf-8"?>
<Network_Records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Networks.xsd">
<NetworkSwitch>
<ChannelID>A</ChannelID>
<SwitchName>OPS1</SwitchName>
<RackNumber>T5553</RackNumber>
<Elevation>A14</Elevation>
<RoomLocation>Phoenix</RoomLocation>
<NetworkSwitchConfigModule>switch_1_phx.txt</NetworkSwitchConfigModule>
<Override>false</Override>
</NetworkSwitch>
<NetworkSwitch>
<ChannelID>A</ChannelID>
<SwitchName>OPS2</SwitchName>
<RackNumber>Fill</RackNumber>
<Elevation>Fill</Elevation>
<RoomLocation>Fill</RoomLocation>
<NetworkSwitchConfigModule>switch_2_cle.txt</NetworkSwitchConfigModule>
<Override>false</Override>
</NetworkSwitch>
文件2-Networks_2.xml
<?xml version="1.0" encoding="utf-8"?>
<Network_Records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Networks.xsd">
<NetworkSwitch>
<ChannelID>A</ChannelID>
<SwitchName>OPS1</SwitchName>
<RackNumber>Fill</RackNumber>
<Elevation>Fill</Elevation>
<RoomLocation>Fill</RoomLocation>
<NetworkModule>
<ModuleNumber>2</ModuleNumber>
<NetworkPort>
<PortNumber>1</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>ASA1 (J5)</DeviceName>
</NetworkPort>
<NetworkPort>
<PortNumber>10</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>CWA2 (J5)</DeviceName>
</NetworkPort>
</NetworkModule>
</NetworkSwitch>
<NetworkSwitch>
<ChannelID>A</ChannelID>
<SwitchName>OPS2</SwitchName>
<RackNumber>REPLACE</RackNumber>
<Elevation>REPLACE</Elevation>
<RoomLocation>REPLACE</RoomLocation>
<NetworkModule>
<ModuleNumber>2</ModuleNumber>
<NetworkPort>
<PortNumber>1</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>ASA1 (J4)</DeviceName>
</NetworkPort>
<NetworkPort>
<PortNumber>10</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>CWA2 (J7)</DeviceName>
</NetworkPort>
</NetworkModule>
</NetworkSwitch>
</Network_Records>
到目前为止我的XSLT-
<xsl:output method="xml" indent="yes" version="1.0" encoding="utf-8" />
<xsl:variable name="tempNetworks" select="document('Networks_2.xml')"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="NetworkSwitchConfigModule">
<xsl:copy-of select="$tempNetworks/Network_Records/NetworkSwitch/NetworkModule"/>
</xsl:template>
<xsl:template match="Override">
</xsl:template>
实际结果-final.xml
<?xml version="1.0" encoding="utf-8"?>
<Network_Records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Networks.xsd">
<NetworkSwitch>
<ChannelID>A</ChannelID>
<SwitchName>OPS1</SwitchName>
<RackNumber>T5553</RackNumber>
<Elevation>A14</Elevation>
<RoomLocation>Phoenix</RoomLocation>
<NetworkModule>
<ModuleNumber>2</ModuleNumber>
<NetworkPort>
<PortNumber>1</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>ASA1 (J5)</DeviceName>
</NetworkPort>
<NetworkPort>
<PortNumber>10</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>CWA2 (J5)</DeviceName>
</NetworkPort>
</NetworkModule>
<NetworkModule>
<ModuleNumber>2</ModuleNumber>
<NetworkPort>
<PortNumber>1</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>ASA1 (J4)</DeviceName>
</NetworkPort>
<NetworkPort>
<PortNumber>10</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>CWA2 (J7)</DeviceName>
</NetworkPort>
</NetworkModule>
</NetworkSwitch>
<NetworkSwitch>
<ChannelID>A</ChannelID>
<SwitchName>OPS2</SwitchName>
<RackNumber>REPLACE</RackNumber>
<Elevation>REPLACE</Elevation>
<RoomLocation>REPLACE</RoomLocation>
<NetworkModule>
<ModuleNumber>2</ModuleNumber>
<NetworkPort>
<PortNumber>1</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>ASA1 (J5)</DeviceName>
</NetworkPort>
<NetworkPort>
<PortNumber>10</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>CWA2 (J5)</DeviceName>
</NetworkPort>
</NetworkModule>
<NetworkModule>
<ModuleNumber>2</ModuleNumber>
<NetworkPort>
<PortNumber>1</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>ASA1 (J4)</DeviceName>
</NetworkPort>
<NetworkPort>
<PortNumber>10</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>CWA2 (J7)</DeviceName>
</NetworkPort>
</NetworkModule>
</NetworkSwitch>
</Network_Records>
预期结果-final.xml
<?xml version="1.0" encoding="utf-8"?>
<Network_Records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Networks.xsd">
<NetworkSwitch>
<ChannelID>A</ChannelID>
<SwitchName>OPS1</SwitchName>
<RackNumber>T5553</RackNumber>
<Elevation>A14</Elevation>
<RoomLocation>Phoenix</RoomLocation>
<NetworkModule>
<ModuleNumber>2</ModuleNumber>
<NetworkPort>
<PortNumber>1</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>ASA1 (J5)</DeviceName>
</NetworkPort>
<NetworkPort>
<PortNumber>10</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>CWA2 (J5)</DeviceName>
</NetworkPort>
</NetworkModule>
</NetworkSwitch>
<NetworkSwitch>
<ChannelID>A</ChannelID>
<SwitchName>OPS2</SwitchName>
<RackNumber>REPLACE</RackNumber>
<Elevation>REPLACE</Elevation>
<RoomLocation>REPLACE</RoomLocation>
<NetworkModule>
<ModuleNumber>2</ModuleNumber>
<NetworkPort>
<PortNumber>1</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>ASA1 (J4)</DeviceName>
</NetworkPort>
<NetworkPort>
<PortNumber>10</PortNumber>
<DeviceType>Processor</DeviceType>
<DeviceName>CWA2 (J7)</DeviceName>
</NetworkPort>
</NetworkModule>
</NetworkSwitch>
</Network_Records>
只需将模板更改为
<xsl:template match="NetworkSwitchConfigModule">
<xsl:copy-of select="$tempNetworks/Network_Records/NetworkSwitch[SwitchName = current()/../SwitchName]/NetworkModule"/>
</xsl:template>
<xsl:template match="Override" />
您一直缺少从第二个XML中选择正确节点的谓词。因此,您复制了所有NetworkModule
元素,而不仅仅是您想要的元素。谓词
SwitchName = current()/../SwitchName
将来自第二文件的CCD_ 2的值与当前匹配的模板规则的父CCD_。