xslt- XSLT 1.0组合选定的属性并使属性具有多值,保留其他属性

时间:2015-07-30 20:22:55

标签: xml xslt

这是我的XML -

 <?xml version="1.0" encoding="UTF-8"?><update-all-attributes>
    <document name="http://blah">
    <price>111 USD</price>
    <color>red</red>
    <size>comfort</size>
    <cost>00012-40</cost>
    <shipping>US::Ground:0.00</shipping>
    <cost>00012-40</cost>
   <price>1999 USD</price>
    <color>black</red>
    <size>Medium</size>
    <cost>00012-40</cost>
    <shipping>US::Ground:0.00</shipping>
    <cost>00012-40</cost>
   <price>19 USD</price>
    <color>blue</red>
    <size>Large</size>
    <shipping>US::Ground:0.00</shipping>
   <price>198 USD</price>
    <color>brown</red>
    <size>Small</size>
    </document>
    </update-all-attributes>

我希望我的最终XML看起来像 -

    <?xml version="1.0" encoding="UTF-8"?>
    <document name="http://blah">
<price>19 USD</price>
    <color>blue</red>
    <size>Large</size>
       <cost>00012-40,00012-40,00012-40,00012-40</cost>
    <price>198 USD</price>
    <color>brown</red>
    <size>Small</size>
     <shipping>US::Ground:0.00,US::Ground:0.00,US::Ground:0.00</shipping>
      <price>111 USD</price>
    <color>red</red>
    <size>comfort</size>
 <price>1999 USD</price>
    <color>black</red>
    <size>Medium</size>
    </document>

这是我一直在尝试的XSLT 1.0 -

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />
 <xsl:strip-space elements="*"/>
    <xsl:key name="gr-cost" match="cost" use="."/>
    <xsl:key name="gr-shipping" match="shipping" use="."/>

    <xsl:template match="update-all-attributes">

          <xsl:apply-templates/>

    </xsl:template>

    <xsl:template match="document">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <cost>
                 <xsl:for-each select="cost[not(following-sibling::cost/text() = ./text())]">
                    <xsl:value-of select="."/>
                    <xsl:if test="position() != last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </cost>
            <shipping>
                <xsl:for-each select="shipping[not(following-sibling::shipping/text() = ./text())]">
                    <xsl:value-of select="."/>
                    <xsl:if test="position() != last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </shipping> 
        </xsl:copy>
    </xsl:template>
</xsl:transform>

这是我得到的输出xml -

<?xml version="1.0" encoding="UTF-8"?>
<document name="http://blah">
   <cost>00012-40</cost>
   <shipping>US::Ground:0.00</shipping>
</document>

如何修改我的XSLT 1.0以组合相似的值并使所选属性具有多值,保留其他属性?

1 个答案:

答案 0 :(得分:1)

你的代码太多了。您所需要的只是:

<xsl:stylesheet version="1.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="*"/>

<xsl:template match="document">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <cost>
             <xsl:for-each select="cost">
                <xsl:value-of select="."/>
                <xsl:if test="position() != last()">
                    <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>
        </cost>
        <shipping>
            <xsl:for-each select="shipping">
                <xsl:value-of select="."/>
                <xsl:if test="position() != last()">
                    <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>
        </shipping> 
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

修改

回复您编辑过的问题:

要保留文档可能包含的任何其他元素,请更改:

<xsl:copy-of select="@*"/>

为:

<xsl:copy-of select="@* | *[not (self::cost or self::shipping)]"/>