【问题标题】:Merging XML files using XSL drops matched element使用 XSL 合并 XML 文件会丢弃匹配的元素
【发布时间】:2015-08-26 22:13:25
【问题描述】:

我一直在使用 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>

请注意,&lt;Shapes&gt;&lt;/Shapes&gt; 元素现已丢失,&lt;Benchtop&gt;&lt;/Benchtop&gt; 元素已被插入。

输出应如下所示:

<?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>

非常感谢任何帮助。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    问题在于模板匹配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>
    

    【讨论】:

    • 太酷了。工作完美。我想这会很简单。非常感谢蒂姆 C
    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 2011-01-17
    相关资源
    最近更新 更多