简体   繁体   中英

Convert xslt/xml separate images

Can anyone help us convert XML code using XSLT 1.0 to XML result.

XML

<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>

Except result:

<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.jpg</ImageURL2>
<ImageURL3>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053.jpg</ImageURL3>
<ImageURL4>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053.jpg</ImageURL4>
<ImageURL5>https://testweb.com/wp-content/uploads/2021/08/DQ5285-53053.jpg</ImageURL5>
</post>
</data>

Please try the following XSLT.

It is not a good practice to put a running number in the XML element name: <ImageURL1>...<ImageURLN>

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

Input XML

<?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>

XSLT

<?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

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM