问题描述
以我的文件为例
<offer>
<products>
<product>
<images>
<large>
<image priority="1" url="data/gfx/pictures/large/9/7/70679_1.jpg" hash="74c757e1dfc9277d6f6ba49c0ca*****" changed="2021-06-18 15:57:02" width="1000" height="1000"/>
<image priority="2" url="data/gfx/pictures/large/9/7/70679_2.jpg" hash="74c757e1dfc9277d6f6ba49c0ca*****" changed="2021-06-18 15:57:02" width="1000" height="1000"/>
<image priority="3" url="data/gfx/pictures/large/9/7/70679_3.jpg" hash="74c757e1dfc9277d6f6ba49c0ca*****" changed="2021-06-18 15:57:02" width="1000" height="1000"/>
</large>
</images>
</product>
</products>
</offer>
我想将所有图片 URL 属性与 /// 结合(可以是任意数量的图片元素) 例如,在下面具有 3 个图像元素的示例中,我需要的结果是:
<offer>
<products>
<product>
<images>
<large>
<image priority="1" url="data/gfx/pictures/large/9/7/70679_1.jpg///data/gfx/pictures/large/9/7/70679_2.jpg///data/gfx/pictures/large/9/7/70679_3.jpg" hash="74c757e1dfc9277d6f6ba49c0ca*****" changed="2021-06-18 15:57:02" width="1000" height="1000"/>
</large>
</images>
</product>
</products>
</offer>
我试过了,但失败了
<?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:template match="/offer/products/product/images/large/image">
<xsl:for-each select="image[*]/@url">
<xsl:value-of select="concat(../../../images/large/image/@path,'///')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这怎么可能?预先感谢您的帮助
解决方法
请尝试以下 XSLT。
Saxon PE 9.9.1.7 符合 XSLT 3.0
string-join()
函数发挥了所有作用。
XSLT
<?xml version='1.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" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="large">
<xsl:copy>
<image priority="1"
url="{string-join((image/@url),'///')}"
hash="{image[1]/@hash}"
changed="{image[1]/@changed}" width="{image[1]/@width}"
height="{image[1]/@height}"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
,
你的例子模棱两可。看看这是否适合您:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="large">
<xsl:copy>
<image>
<xsl:copy-of select="image[1]/@*"/>
<xsl:attribute name="url" select="image/@url" separator="//"/>
</image>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>