Home > other >  Number tags <images> XSLT / XML
Number tags <images> XSLT / XML

Time:10-31

XSLT 2.0 has a built-in function tokenize() for such tasks.

Below is our Input XML code for import products images.

<?xml version="1.0"?>
<data>
    <post>
        <SKU>I-DQ5285 53053</SKU>
        <ImageURL>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053.jpg|https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053_2.jpg|https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053_3.jpg|https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053_4.jpg</ImageURL>
    </post>
</data>

Code XSLT transform

<?xml version="1.0"?>
<xsl:stylesheet version="1.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="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ImageURL">
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="normalize-space(.)"/>
            <xsl:with-param name="elemName" select="'ImageURL'"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="tokenize">
        <xsl:param name="text"/>
        <xsl:param name="elemName"/>
        <xsl:param name="delimiter" select="'|'"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)"/>
        <xsl:if test="$token">
            <xsl:element name="{$elemName}">
                <xsl:value-of select="$token"/>
            </xsl:element>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
                <xsl:with-param name="elemName" select="$elemName"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Output XML. Now I paste result below.

<?xml version="1.0" encoding="utf-8"?>
<data>
  <post>
    <SKU>I-DQ5285 53053</SKU>
    <ImageURL>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053.jpg</ImageURL>
    <ImageURL>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053_2.jpg</ImageURL>
    <ImageURL>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053_3.jpg</ImageURL>
    <ImageURL>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053_4.jpg</ImageURL>
  </post>
</data>

Except result:

<?xml version="1.0" encoding="utf-8"?>
<data>
  <post>
    <SKU>I-DQ5285 53053</SKU>
    <ImageURL1>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053.jpg</ImageURL1>
    <ImageURL2>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053_2.jpg</ImageURL2>
    <ImageURL3>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053_3.jpg</ImageURL3>
    <ImageURL4>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053_4.jpg</ImageURL4>
  </post>
</data>

Can anyone help us to automatically number this one? We will appreciate your help.

CodePudding user response:

Try:

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="elemName"/>
    <xsl:param name="i" select="1"/>
    <xsl:param name="delimiter" select="'|'"/>
    <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)"/>
    <xsl:if test="$token">
        <xsl:element name="{$elemName}{$i}">
            <xsl:value-of select="$token"/>
        </xsl:element>
    </xsl:if>
    <xsl:if test="contains($text, $delimiter)">
        <!-- recursive call -->
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            <xsl:with-param name="elemName" select="$elemName"/>
            <xsl:with-param name="i" select="$i   1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>
  • Related