0

I want to convert & compatible the below XML file structure with Magento 2 via XSLT, I start handling it but cannot complete it.

product.xml

<products>
<product>
    <id>234234</id>
    <code>TEST</code>
    <price>16</price>
    <stockamount>2983</stockamount>
    <catalog>
        <![CDATA[ A ]]>
    </catalog>
    <category>
        <![CDATA[ B ]]>
    </category>
    <subcategory>
        <![CDATA[ C ]]>
    </subcategory>
    <color>Black</color>
    <description>
        <![CDATA[ ]]>
    </description>
    <fabric>
        <![CDATA[ ]]>
    </fabric>
    <published>2020-03-04 20:05:47</published>
    <sizes>
        <size>
            <name>55</name>
            <amount>997</amount>
        </size>
        <size>
            <name>50</name>
            <amount>993</amount>
        </size>
        <size>
            <name>48</name>
            <amount>993</amount>
        </size>
    </sizes>
    <images>
        <image>http://example.com/1.jpg</image>
        <image>http://example.com/2.jpg</image>
        <image>http://example.com/3.jpg</image>
        <image>http://example.com/4.jpg</image>
    </images>
</product>

XSLT:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:strip-space elements="description fabric catalog category subcategory" />

<xsl:template match="products">
    <xsl:element name="product">
        <xsl:for-each select="product">
            <xsl:element name="sku">
                <xsl:value-of select="id"/>
            </xsl:element>
            <xsl:element name="name">
                <xsl:value-of select="code"/>
            </xsl:element>
            <xsl:element name="price">
                <xsl:value-of select="price"/>
            </xsl:element>
            <xsl:element name="qty">
                <xsl:value-of select="stockamount"/>
            </xsl:element>
            <xsl:element name="color">
                <xsl:value-of select="color"/>
            </xsl:element>
            <xsl:element name="description">
                <xsl:value-of select="description"/>
            </xsl:element>
            <xsl:element name="fabric">
                <xsl:value-of select="fabric"/>
            </xsl:element>
            <xsl:element name="categories">
                <xsl:value-of select="normalize-space(concat(catalog,'/',category,'/',subcategory))"/>
            </xsl:element>
        </xsl:for-each>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

CurrentXMLResult:

<?xml version="1.0" encoding="utf-8"?>
<product>
<sku>234234</sku>
<name>TEST</name>
<price>16</price>
<qty>2983</qty>
<color>Black</color>
<description></description>
<fabric></fabric>
<categories>Women / Clothes / Dresses</categories>
</product>

ExpectedXMLResult:

<?xml version="1.0" encoding="utf-8"?>
<product>
<sku>132320</sku>
<name>BBT102103-B2</name>
<price>16</price>
<qty>2983</qty>
<color>Black</color>
<description></description>
<fabric></fabric>
**<categories>Women/Clothes/Dresses</categories>**
<base_image>http://example.com/1.jpg</base_image>
<small_image>http://example.com/2.jpg</small_image>
<thumbnail_image>http://example.com/3.jpg</thumbnail_image>
<additional_image>http://example.com/4.jpg</additional_image>
size as configurable_variations etc...
</product>

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.