Sioulis Nick
Sioulis Nick

Reputation: 17

XSLT 3.0 How to String joing specific element texts in one element

Hi I have this sample that contains multiple products each with element <main_image>text</main_image> and then each have additional elements <image_>text...</image_>. I want to merge them all to elements that contain <image_*> to the <main_image> separated by comma (,).

    <shop>
  <products>
    <product>
      <price>176.5500</price>
      <pricecustomer>154.4812</pricecustomer>
      <product_id>6167</product_id>
      <model>BUN-001</model>
      <ean></ean>
      <mpn>BUN-001</mpn>
      <isbn>0</isbn>
      <minimum>1</minimum>
      <tax_class>1</tax_class>
      <quantity>0</quantity>
      <main_image>https://www.test.com/image/products/Bundles/bundle-001.jpg</main_image>
      <manufacturer></manufacturer>
      <varos>0.00000000</varos>
      <mikos>0.00000000</mikos>
      <platos>0.00000000</platos>
      <ipsos>0.00000000</ipsos>
      <status>Published</status>
      <image_1>https://www.test.com/image/products/beper/bt.200/BT.200.jpg</image_1>
      <image_2>https://www.test.com/image/products/beper/bt.200/BT.200-1.jpg</image_2>
      <image_3>https://www.test.com/image/products/zilan/zln7887/ZLN7887.jpg</image_3>             
    </product>
  </products>
</shop>

and the result that i want is this

<shop>
  <products>
    <product>
      <price>176.5500</price>
      <pricecustomer>154.4812</pricecustomer>
      <product_id>6167</product_id>
      <model>BUN-001</model>
      <ean></ean>
      <mpn>BUN-001</mpn>
      <isbn>0</isbn>
      <minimum>1</minimum>
      <tax_class>1</tax_class>
      <quantity>0</quantity>
      <main_image>https://www.test.com/image/products/Bundles/bundle-001.jpg,https://www.test.com/image/products/beper/bt.200/BT.200.jpg,https://www.test.com/image/products/beper/bt.200/BT.200-1.jpg,https://www.test.com/image/products/zilan/zln7887/ZLN7887.jpg</main_image>
      <manufacturer></manufacturer>
      <varos>0.00000000</varos>
      <mikos>0.00000000</mikos>
      <platos>0.00000000</platos>
      <ipsos>0.00000000</ipsos>
      <status>Published</status>
      <image_1>https://www.test.com/image/products/beper/bt.200/BT.200.jpg</image_1>
      <image_2>https://www.test.com/image/products/beper/bt.200/BT.200-1.jpg</image_2>
      <image_3>https://www.test.com/image/products/zilan/zln7887/ZLN7887.jpg</image_3>
          
    </product>
  </products>
</shop>

I have tried this code snippet with XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   exclude-result-prefixes="xs"
   version="2.0">
   
   <xsl:mode on-no-match="shallow-copy"/>
   
   <xsl:template match="main_image">
      
         <xsl:value-of select="if (../../../main_image/text()) then string-join((image_*/text()), ',') else ''"/>            
         <xsl:value-of select="."/>
      
   </xsl:template>
   
</xsl:stylesheet>

That results no main image at all, what do I miss? Thank you for any help

Upvotes: 0

Views: 24

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167571

You can use

   <xsl:template match="main_image">
      <xsl:copy>
         <xsl:value-of select="., ../*[matches(local-name(), '^image_')]" separator=","/>            
      </xsl:copy>      
   </xsl:template>

Upvotes: 1

Related Questions