微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XSL 循环遍历 XML 元素的属性,然后用连接的属性替换第一个元素

如何解决XSL 循环遍历 XML 元素的属性,然后用连接的属性替换第一个元素

以我的文件为例

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。