Home > other >  XSLT how to remove the repeated output
XSLT how to remove the repeated output

Time:11-01

This is my xml source file.

<?xml version="1.0" encoding="UTF-8"?>
<ZEQUIPMENT_SVMX01_01>
    <IDOC BEGIN="1">
        <Z1EQUIPMENT_SVMX SEGMENT="1">
            <Z1CAWNM_SVMX SEGMENT="1">
                <ATNAM>TS_DEVICE_USAGE_DEPARTMENT</ATNAM>
                <ATWRT>ABC</ATWRT>
                <ATZHL> 1</ATZHL>
            </Z1CAWNM_SVMX>
        </Z1EQUIPMENT_SVMX>
    </IDOC>
    <IDOC BEGIN="1">
        <Z1EQUIPMENT_SVMX SEGMENT="1">
            <Z1CAWNM_SVMX SEGMENT="1">
                <ATNAM>TS_DEVICE_USAGE_DEPARTMENT</ATNAM>
                <ATWRT>LOA</ATWRT>
                <ATZHL> 1</ATZHL>
            </Z1CAWNM_SVMX>
            <Z1CAWNM_SVMX SEGMENT="1">
                <ATNAM>TS_DEVICE_USAGE_DEPARTMENT</ATNAM>
                <ATWRT>VET</ATWRT>
                <ATZHL> 2</ATZHL>
            </Z1CAWNM_SVMX>
        </Z1EQUIPMENT_SVMX>
    </IDOC>
</ZEQUIPMENT_SVMX01_01>

and below is the output after my xslt mapping. as you can see there are 2 repeat line and my expected result is only 1 line. I have already spend a few day trying to solve the issue but until now still no idea how to remove it. any helps is much appreciate!!

<?xml version="1.0" encoding="UTF-8"?>
<a:Upsert_Installed_Product_Buffer_Object__cBulkRequest xmlns:a="http:///10" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <batch>
      <sObject>
         <R3_DEVICE_USAGE_DEPARTMENT__c>ABC</R3_DEVICE_USAGE_DEPARTMENT__c>
      </sObject>
      <sObject>
         <R3_DEVICE_USAGE_DEPARTMENT__c>LOA;VET</R3_DEVICE_USAGE_DEPARTMENT__c>
         <R3_DEVICE_USAGE_DEPARTMENT__c>LOA;VET</R3_DEVICE_USAGE_DEPARTMENT__c>
      </sObject>
   </batch>
</a:Upsert_Installed_Product_Buffer_Object__cBulkRequest>

here is my xslt mapping:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http:///10" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <a:Upsert_Installed_Product_Buffer_Object__cBulkRequest>
            <batch>
                <xsl:choose>
                    <xsl:when test="ZEQUIPMENT_SVMX01">
                        <xsl:apply-templates select="ZEQUIPMENT_SVMX01/IDOC"/>
                    </xsl:when>
                    <xsl:when test="ZEQUIPMENT_SVMX01_01">
                        <xsl:apply-templates select="ZEQUIPMENT_SVMX01_01/IDOC"/>
                    </xsl:when>
                </xsl:choose>
            </batch>
        </a:Upsert_Installed_Product_Buffer_Object__cBulkRequest>
    </xsl:template>
    <xsl:template match="IDOC">
        <sObject>
            <xsl:apply-templates select="Z1EQUIPMENT_SVMX/Z1CAWNM_SVMX[not(ATNAM=preceding::ATNAM[position()=0])]"/>
        </sObject>
    </xsl:template>
    <xsl:template match="Z1CAWNM_SVMX">
        <xsl:variable name="ChararcteristicName" select="ATNAM"/>
        <xsl:element name="{concat('R3_',substring-after(ATNAM,'TS_'),'__c')}">
            <xsl:apply-templates select="../Z1CAWNM_SVMX[ATNAM = $ChararcteristicName]/ATWRT"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="ATWRT">
        <xsl:value-of select="."/>
        <xsl:if test="position() != last()">
            <xsl:text>;</xsl:text>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

CodePudding user response:

I think your problem is in the XPath expression

Z1EQUIPMENT_SVMX/Z1CAWNM_SVMX[not(ATNAM=preceding::ATNAM[position()=0])]

I think the predicate [not(ATNAM=preceding::ATNAM[position()=0])] is your attempt to avoid duplicating the data?

If so, you should realise that none of the Z1EQUIPMENT_SVMX/Z1CAWNM_SVMX elements have a preceding::ATNAM; perhaps you meant to write preceding::Z1CAWNM_SVMX/ATNAM? But the logic isn't clear to me.

CodePudding user response:

Try perhaps something along the lines of:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http:///10" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/ZEQUIPMENT_SVMX01_01">
    <a:Upsert_Installed_Product_Buffer_Object__cBulkRequest >
        <batch>
            <xsl:for-each select="IDOC">
                <sObject>
                    <R3_DEVICE_USAGE_DEPARTMENT__c>
                        <xsl:for-each select="Z1EQUIPMENT_SVMX/Z1CAWNM_SVMX">
                            <xsl:value-of select="ATWRT"/>
                            <xsl:if test="position()!=last()">;</xsl:if>
                        </xsl:for-each>
                    </R3_DEVICE_USAGE_DEPARTMENT__c>
                </sObject>
            </xsl:for-each> 
        </batch>
    </a:Upsert_Installed_Product_Buffer_Object__cBulkRequest>
</xsl:template>

</xsl:stylesheet>

(Purposefully simplified to focus on what seems to be the main issue here.)

  • Related