Concatenate XML Files Using XSL Mapping Element

I am working on merging XML files using XSL and have a problem.

The element I want to insert new xml in <Shapes></Shapes>

is discarded and a new unwanted element is injected.

Here's the XSL I'm using:

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

      

Here is the first XML file:

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

      

and here's the second XML file:

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

      

and here's the XML I need to insert:

             <!--Shape-->
              <Shape Id="1">
                <!--Material-->
                <Material Class_Code="MATCL010006"/>
                <Principal_Structure Id="1">
                  <!--Polygon-->
                </Principal_Structure>
                <LayerTexts>
                  <!--Texts for material and thickness-->
                </LayerTexts>
              </Shape>

      

The result is:

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

      

Note that the item is <Shapes></Shapes>

missing and the item has <Benchtop></Benchtop>

been inserted.

The result should look like this:

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

      

Any help is greatly appreciated.

+3


source to share


1 answer


The problem is pattern matching 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>

      

In particular, the position xsl:copy

. Internally, xsl:for-each

you changed the context and are now positioned on the element Benchtop

. Therefore, xsl:copy

this control will be copied.



You need to move xsl:copy

out xsl:for-each

, so copy the element if necessaryShapes

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

      

+2


source







All Articles