XSLT 将嵌套的 xml 元素写为一个元素,并使用逗号分隔值字符串

XSLT rewrite nested xml elements as one element with a comma seperated value string

我想使用 xslt

将所有 属性 元素转换为逗号分隔的字符串作为 xml 中的一个元素
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Products>
<Product>
<productId>1</productId>
<ean>12345</ean>
<title>title A</title>
<Properties>
<Property><key>Colour</key><value>Red</value></Property>
<Property><key>Material</key><value>Plastic</value></Property>
</Properties>
</Product>
<Product>
<productId>2</productId>
<ean>54321</ean>
<title>title B</title>
<Properties>
<Property><key>Colour</key><value>Black</value></Property>
<Property><key>Gender</key><value>Boys</value></Property>
<Property><key>Material</key><value>Leather</value></Property>
</Properties>
</Product>
</Products>

需要使用 XSLT 的输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Products>
<Product>
<productId>1</productId>
<ean>12345</ean>
<title>title A</title>
<Properties><Property>Colour:Red,Material:Plastic</Property></Properties></Product>
<Product>
<productId>2</productId>
<ean>54321</ean>
<title>title B</title>
<Properties><Property>Colour:Black,Gender:Boys,Material:Leather</Property></Properties>
</Product>
</Products>

结果xml 被展平,没有嵌套结构。如果有人有更好的主意将 属性 类型作为元素名称,那就更好了。

如果有可能得到:

<Properties><Colour>Black</Colour><Gender>Boys</Gender><Material>Leather</Material></Properties>

希望收到你的来信!

请尝试以下解决方案。

输入XML

<?xml version="1.0"?>
<Products>
    <Product>
        <productId>1</productId>
        <ean>12345</ean>
        <title>title A</title>
        <Properties>
            <Property>
                <key>Colour</key>
                <value>Red</value>
            </Property>
            <Property>
                <key>Material</key>
                <value>Plastic</value>
            </Property>
        </Properties>
    </Product>
    <Product>
        <productId>2</productId>
        <ean>54321</ean>
        <title>title B</title>
        <Properties>
            <Property>
                <key>Colour</key>
                <value>Black</value>
            </Property>
            <Property>
                <key>Gender</key>
                <value>Boys</value>
            </Property>
            <Property>
                <key>Material</key>
                <value>Leather</value>
            </Property>
        </Properties>
    </Product>
</Products>

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" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Property">
        <xsl:element name="{key}">
            <xsl:value-of select="value"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

输出XML

<Products>
  <Product>
    <productId>1</productId>
    <ean>12345</ean>
    <title>title A</title>
    <Properties>
      <Colour>Red</Colour>
      <Material>Plastic</Material>
    </Properties>
  </Product>
  <Product>
    <productId>2</productId>
    <ean>54321</ean>
    <title>title B</title>
    <Properties>
      <Colour>Black</Colour>
      <Gender>Boys</Gender>
      <Material>Leather</Material>
    </Properties>
  </Product>
</Products>