[英]Convert xslt/xml separate images
Can anyone help us convert XML code using XSLT 1.0 to XML result.任何人都可以帮助我们将使用 XSLT 1.0 的 XML 代码转换为 XML 结果。
XML 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.请尝试以下 XSLT。
It is not a good practice to put a running number in the XML element name: <ImageURL1>...<ImageURLN>
在 XML 元素名称中放置一个编号不是一个好习惯:
<ImageURL1>...<ImageURLN>
XSLT 2.0 has a built-in function tokenize()
for such tasks. XSLT 2.0 具有用于此类任务的内置函数
tokenize()
。
Input XML输入 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 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
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.