I have difficulties to fix the last part of my xml transform. Any help welcome. Also created a fiddle: https://xsltfiddle.liberty-development.net/93FcQpw
My xml:
<products>
<product ID="14352">
<name>product1 | Small</name>
<URL>url1</URL>
<images>
<image>image1</image>
</images>
<description><![CDATA[desc]]></description>
<categories>
<category path="cat">Category1</category>
</categories>
<properties>
<property name="categoryPath">
<value>Huis en tuin > Planten > Bloemen</value>
</property>
<property name="EAN">
</property>
<property name="fromPrice">
<value>18.95</value>
</property>
<property name="imageURL_large">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</value>
</property>
<property name="imageURL_small">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx1.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx2.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx3.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx4.png</value>
</property>
<property name="subcategories">
<value>sub1</value>
</property>
</properties>
</product>
<product ID="13276">
<name>product2 | Small</name>
<URL>url2</URL>
<images>
<image>image2</image>
</images>
<description><![CDATA[desc]]></description>
<categories>
<category path="cat">Category2</category>
</categories>
<properties>
<property name="categoryPath">
<value>A > B > C</value>
</property>
<property name="EAN">
</property>
<property name="fromPrice">
<value>12.85</value>
</property>
<property name="imageURL_large">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yyyy-Boven.png</value>
</property>
<property name="imageURL_small">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy1.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy2.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy3.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy4.png</value>
</property>
<property name="subcategories">
<value>sub2</value>
</property>
</properties>
</product>
<product ID="11084">
<name>product2 | Small</name>
<URL>url2</URL>
<images>
<image>image2</image>
</images>
<description><![CDATA[desc]]></description>
<categories>
<category path="cat">Category2</category>
</categories>
<properties>
<property name="categoryPath">
<value>A > B > C</value>
</property>
<property name="EAN">
</property>
<property name="fromPrice">
<value>12.85</value>
</property>
<property name="imageURL_large">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</value>
</property>
<property name="imageURL_small">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz1.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz2.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz3.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz4.png</value>
</property>
<property name="subcategories">
<value>sub2</value>
</property>
</properties>
</product>
</products>
The xsl I have created so far:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="images/*">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="local-name()"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="categories/*">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="local-name()"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="property">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="@name"/>_s</xsl:attribute>
<xsl:value-of select="value"/>
</xsl:element>
<xsl:apply-templates select="property"/>
</xsl:template>
<xsl:template match="product/*[not(self::properties | self::categories | self::images)]">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="local-name()"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match='/products'>
<add>
<xsl:apply-templates select="product"/>
</add>
</xsl:template>
<xsl:template match="product">
<xsl:variable name="pos" select="position()"/>
<doc>
<xsl:apply-templates>
<xsl:with-param name="pos"><xsl:value-of select="$pos"/></xsl:with-param>
</xsl:apply-templates>
</doc>
</xsl:template>
</xsl:stylesheet>
The current output is missing the ID attribute and I also want a ; seperated list of image urls:
<?xml version="1.0" encoding="utf-8"?>
<add>
<doc>
<field name="ID">14352</field>
<field name="name">product1 | Small</field>
<field name="URL">url1</field>
<field name="image">image1</field>
<field name="description">desc</field>
<field name="category">Category1</field>
<field name="categoryPath_s">Huis en tuin > Planten > Bloemen</field>
<field name="EAN_s"/>
<field name="fromPrice_s">18.95</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxx1.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx2.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx3.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx4.png</field>
<field name="subcategories_s">sub1</field>
</doc>
<doc>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/yyyy-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/yy1.png https://www.xxx.nl/wp-content/uploads/2021/05/yy2.png https://www.xxx.nl/wp-content/uploads/2021/05/yy3.png https://www.xxx.nl/wp-content/uploads/2021/05/yy4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
<doc>
<field name="ID">13276</field>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/zzz1.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz2.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz3.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
</add>
Wanted output, look for the ID attribute as element and the ; seperated list of imageURL_small_s element:
<?xml version="1.0" encoding="utf-8"?>
<add>
<doc>
<field name="ID">11084</field>
<field name="name">product1 | Small</field>
<field name="URL">url1</field>
<field name="image">image1</field>
<field name="description">desc</field>
<field name="category">Category1</field>
<field name="categoryPath_s">Huis en tuin > Planten > Bloemen</field>
<field name="EAN_s"/>
<field name="fromPrice_s">18.95</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxx1.png ; https://www.xxx.nl/wp-content/uploads/2021/05/xxx2.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx3.png ; https://www.xxx.nl/wp-content/uploads/2021/05/xxx4.png</field>
<field name="subcategories_s">sub1</field>
</doc>
<doc>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/yyyy-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/yy1.png ; https://www.xxx.nl/wp-content/uploads/2021/05/yy2.png https://www.xxx.nl/wp-content/uploads/2021/05/yy3.png ; https://www.xxx.nl/wp-content/uploads/2021/05/yy4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
<doc>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/zzz1.png ; https://www.xxx.nl/wp-content/uploads/2021/05/zzz2.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz3.png ; https://www.xxx.nl/wp-content/uploads/2021/05/zzz4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
</add>
CodePudding user response:
I believe this could be much simpler:
XSLT 3.0
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
expand-text="yes">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/products">
<add>
<xsl:apply-templates/>
</add>
</xsl:template>
<xsl:template match="product">
<doc>
<field name="ID">{@ID}</field>
<xsl:apply-templates/>
</doc>
</xsl:template>
<xsl:template match="*[text()]">
<field name="{name()}">{.}</field>
</xsl:template>
<xsl:template match="property">
<field name="{@name}">
<xsl:value-of select="value" separator=" ; "/>
</field>
</xsl:template>
</xsl:stylesheet>
Demo: https://xsltfiddle.liberty-development.net/93FcQpw/2
CodePudding user response:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" expand-text="yes">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/products">
<add>
<xsl:apply-templates/>
</add>
</xsl:template>
<xsl:template match="product">
<doc>
<field name="id"><xsl:value-of select="@ID"/></field>
<xsl:apply-templates/>
</doc>
</xsl:template>
<xsl:template match="*[text()]">
<field name="{name()}_s"><xsl:value-of select="."/></field>
</xsl:template>
<xsl:template match="property">
<field name="{@name}_s">
<xsl:value-of select="value" separator=" ; "/>
</field>
</xsl:template>
</xsl:stylesheet>