我一直在使用XSL合并XML文件并遇到问题。
我希望将新xml插入<Shapes></Shapes>
的元素被删除,并且正在引入一个新的不需要的元素。
这是我正在使用的XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
extension-element-prefixes="msxsl" >
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Shapes">
<xsl:for-each select="document('..\TempReportData\TextXML_Output.xml')/Job/Benchtops/Benchtop">
<xsl:copy>
<xsl:apply-templates select="document('..\DesignMaster\EmptyShapeElement.xml')" />
</xsl:copy>
</xsl:for-each>
</xsl:template>
这是第一个XML文件:
<?xml version="1.0"?>
<EXPORT>
<CAM>
<Version V="3_0_0">
<Project Computer_Name="CW032" User_Name="Sflores">
<!--Offer-->
<Offer Code="J021368">
<!--Client-->
<Customer Code="8"/>
<!--Top-->
<Top Id="1">
<Shapes>
</Shapes>
<Texts>
</Texts>
<TextsLav>
<!--Working Texts-->
</TextsLav>
<Dimensions>
</Dimensions>
<FreeElements>
<!--Elementi senza assegnazione-->
</FreeElements>
</Top>
</Offer>
</Project>
</Version>
</CAM>
</EXPORT>
并且这是第二个XML文件:
<?xml version="1.0"?>
<Job>
<Job_Number>B90512</Job_Number>
<Job_Address></Job_Address>
<Benchtops>
<Benchtop>
<Cabinet_Type>Benchtop</Cabinet_Type>
<Page>Page 1</Page>
<Room>Kitchen</Room>
<Top_number>TOP(2257)</Top_number>
<Length>2641mm</Length>
<Width>800mm</Width>
<Width2>2641mm</Width2>
</Benchtop>
<Benchtop>
<Cabinet_Type>Benchtop</Cabinet_Type>
<Page>Page 1</Page>
<Room>Kitchen</Room>
<Top_number>TOP(2260)</Top_number>
<Length>2772mm</Length>
<Width>600mm</Width>
<Width2>2772mm</Width2>
</Benchtop>
</Benchtops>
</Job>
以及我需要插入的XML:
<!--Shape-->
<Shape Id="1">
<!--Material-->
<Material Class_Code="MATCL010006"/>
<Principal_Structure Id="1">
<!--Polygon-->
</Principal_Structure>
<LayerTexts>
<!--Texts for material and thickness-->
</LayerTexts>
</Shape>
我得到的结果是:
<?xml version="1.0"?>
<EXPORT>
<CAM>
<Version V="3_0_0">
<Project Computer_Name="CW032" User_Name="Sflores">
<!--Offer-->
<Offer Code="B90512">
<!--Client-->
<Customer Code="8"></Customer>
<!--Top-->
<Top Id="1">
<Benchtop>
<!--Shape-->
<Shape Id="1">
<!--Material-->
<Material Class_Code="MATCL010006"></Material>
<Principal_Structure Id="1">
<!--Polygon-->
</Principal_Structure>
<LayerTexts>
<!--Texts for material and thickness-->
</LayerTexts>
</Shape>
</Benchtop>
<Benchtop>
<!--Shape-->
<Shape Id="1">
<!--Material-->
<Material Class_Code="MATCL010006"></Material>
<Principal_Structure Id="1">
<!--Polygon-->
</Principal_Structure>
<LayerTexts>
<!--Texts for material and thickness-->
</LayerTexts>
</Shape>
</Benchtop>
<Texts></Texts>
<TextsLav>
<!--Working Texts-->
</TextsLav>
<Dimensions></Dimensions>
<FreeElements>
<!--Elementi senza assegnazione-->
</FreeElements>
</Top>
</Offer>
</Project>
</Version>
</CAM>
请注意,现在缺少<Shapes></Shapes>
元素,并且已插入<Benchtop></Benchtop>
元素。
输出应如下所示:
<?xml version="1.0"?>
<EXPORT>
<CAM>
<Version V="3_0_0">
<Project Computer_Name="CW032" User_Name="Sflores">
<!--Offer-->
<Offer Code="B90512">
<!--Client-->
<Customer Code="8"></Customer>
<!--Top-->
<Top Id="1">
<Shapes>
<!--Shape-->
<Shape Id="1">
<!--Material-->
<Material Class_Code="MATCL010006"></Material>
<Principal_Structure Id="1">
<!--Polygon-->
</Principal_Structure>
<LayerTexts>
<!--Texts for material and thickness-->
</LayerTexts>
</Shape>
<!--Shape-->
<Shape Id="1">
<!--Material-->
<Material Class_Code="MATCL010006"></Material>
<Principal_Structure Id="1">
<!--Polygon-->
</Principal_Structure>
<LayerTexts>
<!--Texts for material and thickness-->
</LayerTexts>
</Shape>
</Shapes>
<Texts></Texts>
<TextsLav>
<!--Working Texts-->
</TextsLav>
<Dimensions></Dimensions>
<FreeElements>
<!--Elementi senza assegnazione-->
</FreeElements>
</Top>
</Offer>
</Project>
</Version>
</CAM>
非常感谢任何帮助。
答案 0 :(得分:2)
问题出在模板匹配Shapes
<xsl:template match="Shapes">
<xsl:for-each select="document('..\TempReportData\TextXML_Output.xml')/Job/Benchtops/Benchtop">
<xsl:copy>
<xsl:apply-templates select="document('..\DesignMaster\EmptyShapeElement.xml')" />
</xsl:copy>
</xsl:for-each>
</xsl:template>
特别是xsl:copy
的位置。在xsl:for-each
内您已更改了上下文,现在位于Benchtop
元素上。因此,xsl:copy
将复制该基准元素。
您需要将xsl:copy
移到xsl:for-each
之外,以便根据需要复制Shapes
元素
<xsl:template match="Shapes">
<xsl:copy>
<xsl:for-each select="document('..\TempReportData\TextXML_Output.xml')/Job/Benchtops/Benchtop">
<xsl:apply-templates select="document('..\DesignMaster\EmptyShapeElement.xml')" />
</xsl:for-each>
</xsl:copy>
</xsl:template>