xslt- XSLT 1.0 to combine selected attributes and

2019-08-08 10:01发布

This is my 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>

I want my final XML to look like-

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

This is the XSLT 1.0 I have been trying with-

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

And this is the output xml I am getting-

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

How can I modify my XSLT 1.0 to combine similar values and make the selected attribute muti-valued, retaining the other attributes as they are?

标签: xml xslt
1条回答
Juvenile、少年°
2楼-- · 2019-08-08 10:46

You have too much code. All you need is:

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

Edit

In response to your edited question:

To keep any other element a document might have, change:

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

to:

<xsl:copy-of select="@* | *[not (self::cost or self::shipping)]"/>
查看更多
登录 后发表回答