XSLT 3.0 如何在一个元素中连接特定元素文本
XSLT 3.0 How to String joing specific element texts in one element
您好,我有这个示例,其中包含多个产品,每个产品都带有元素 text ,然后每个都有附加元素 >text.. . >.
我想将它们全部合并到包含 到以逗号 (,) 分隔的 的元素。
<shop>
<products>
<product>
<price>176.5500</price>
<pricecustomer>154.4812</pricecustomer>
<product_id>6167</product_id>
<model>BUN-001</model>
<ean></ean>
<mpn>BUN-001</mpn>
<isbn>0</isbn>
<minimum>1</minimum>
<tax_class>1</tax_class>
<quantity>0</quantity>
<main_image>https://www.test.com/image/products/Bundles/bundle-001.jpg</main_image>
<manufacturer></manufacturer>
<varos>0.00000000</varos>
<mikos>0.00000000</mikos>
<platos>0.00000000</platos>
<ipsos>0.00000000</ipsos>
<status>Published</status>
<image_1>https://www.test.com/image/products/beper/bt.200/BT.200.jpg</image_1>
<image_2>https://www.test.com/image/products/beper/bt.200/BT.200-1.jpg</image_2>
<image_3>https://www.test.com/image/products/zilan/zln7887/ZLN7887.jpg</image_3>
</product>
</products>
</shop>
我想要的结果是这个
<shop>
<products>
<product>
<price>176.5500</price>
<pricecustomer>154.4812</pricecustomer>
<product_id>6167</product_id>
<model>BUN-001</model>
<ean></ean>
<mpn>BUN-001</mpn>
<isbn>0</isbn>
<minimum>1</minimum>
<tax_class>1</tax_class>
<quantity>0</quantity>
<main_image>https://www.test.com/image/products/Bundles/bundle-001.jpg,https://www.test.com/image/products/beper/bt.200/BT.200.jpg,https://www.test.com/image/products/beper/bt.200/BT.200-1.jpg,https://www.test.com/image/products/zilan/zln7887/ZLN7887.jpg</main_image>
<manufacturer></manufacturer>
<varos>0.00000000</varos>
<mikos>0.00000000</mikos>
<platos>0.00000000</platos>
<ipsos>0.00000000</ipsos>
<status>Published</status>
<image_1>https://www.test.com/image/products/beper/bt.200/BT.200.jpg</image_1>
<image_2>https://www.test.com/image/products/beper/bt.200/BT.200-1.jpg</image_2>
<image_3>https://www.test.com/image/products/zilan/zln7887/ZLN7887.jpg</image_3>
</product>
</products>
</shop>
我已经用 XSLT 尝试过这段代码
<?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:mode on-no-match="shallow-copy"/>
<xsl:template match="main_image">
<xsl:value-of select="if (../../../main_image/text()) then string-join((image_*/text()), ',') else ''"/>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
结果根本没有主图像,我错过了什么?感谢您的帮助
您可以使用
<xsl:template match="main_image">
<xsl:copy>
<xsl:value-of select="., ../*[matches(local-name(), '^image_')]" separator=","/>
</xsl:copy>
</xsl:template>
您好,我有这个示例,其中包含多个产品,每个产品都带有元素
<shop>
<products>
<product>
<price>176.5500</price>
<pricecustomer>154.4812</pricecustomer>
<product_id>6167</product_id>
<model>BUN-001</model>
<ean></ean>
<mpn>BUN-001</mpn>
<isbn>0</isbn>
<minimum>1</minimum>
<tax_class>1</tax_class>
<quantity>0</quantity>
<main_image>https://www.test.com/image/products/Bundles/bundle-001.jpg</main_image>
<manufacturer></manufacturer>
<varos>0.00000000</varos>
<mikos>0.00000000</mikos>
<platos>0.00000000</platos>
<ipsos>0.00000000</ipsos>
<status>Published</status>
<image_1>https://www.test.com/image/products/beper/bt.200/BT.200.jpg</image_1>
<image_2>https://www.test.com/image/products/beper/bt.200/BT.200-1.jpg</image_2>
<image_3>https://www.test.com/image/products/zilan/zln7887/ZLN7887.jpg</image_3>
</product>
</products>
</shop>
我想要的结果是这个
<shop>
<products>
<product>
<price>176.5500</price>
<pricecustomer>154.4812</pricecustomer>
<product_id>6167</product_id>
<model>BUN-001</model>
<ean></ean>
<mpn>BUN-001</mpn>
<isbn>0</isbn>
<minimum>1</minimum>
<tax_class>1</tax_class>
<quantity>0</quantity>
<main_image>https://www.test.com/image/products/Bundles/bundle-001.jpg,https://www.test.com/image/products/beper/bt.200/BT.200.jpg,https://www.test.com/image/products/beper/bt.200/BT.200-1.jpg,https://www.test.com/image/products/zilan/zln7887/ZLN7887.jpg</main_image>
<manufacturer></manufacturer>
<varos>0.00000000</varos>
<mikos>0.00000000</mikos>
<platos>0.00000000</platos>
<ipsos>0.00000000</ipsos>
<status>Published</status>
<image_1>https://www.test.com/image/products/beper/bt.200/BT.200.jpg</image_1>
<image_2>https://www.test.com/image/products/beper/bt.200/BT.200-1.jpg</image_2>
<image_3>https://www.test.com/image/products/zilan/zln7887/ZLN7887.jpg</image_3>
</product>
</products>
</shop>
我已经用 XSLT 尝试过这段代码
<?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:mode on-no-match="shallow-copy"/>
<xsl:template match="main_image">
<xsl:value-of select="if (../../../main_image/text()) then string-join((image_*/text()), ',') else ''"/>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
结果根本没有主图像,我错过了什么?感谢您的帮助
您可以使用
<xsl:template match="main_image">
<xsl:copy>
<xsl:value-of select="., ../*[matches(local-name(), '^image_')]" separator=","/>
</xsl:copy>
</xsl:template>