xslt 1.0- Using xslt 1.0 how to strip the duplicat

2019-08-10 01:53发布

This is my XML-

<?xml version="1.0" encoding="UTF-8"?>
    <update-all-attributes>
      <document name="http://blah">
        <price>111 USD</price>
        <color>red</color>
        <size>comfort</size>
        <cost>00012-40</cost>
        <shipping>US::Ground:0.00</shipping>
        <cost>00012-40</cost>
        <price>111 USD</price>
        <color>red</color>
        <size>comfort</size>
        <cost>00012-40</cost>
        <shipping>US::Ground:0.00</shipping>
        <cost>00012-40</cost>
        <price>111 USD</price>
        <color>red</color>
        <size>comfort</size>
        <shipping>US::Ground:0.00</shipping>
        <price>111 USD</price>
        <color>red</color>
        <size>comfort</size>
      </document>
      <document name="http://blahblah">
        <price>110 USD</price>
        <color>blue</color>
        <size>super</size>
        <cost>00012-41</cost>
        <shipping>US::Ground:0.01</shipping>
        <cost>00012-41</cost>
        <price>110 USD</price>
        <color>blue</color>
        <size>super</size>
        <shipping>US::Ground:0.01</shipping>
        <cost>00012-41</cost>
        <price>110 USD</price>
        <color>blue</color>
        <size>super</size>
        <shipping>US::Ground:0.01</shipping>
        <price>110 USD</price>
        <color>blue</color>
        <size>super</size>
      </document>
    </update-all-attributes>

I want my final XML as

<?xml version="1.0" encoding="UTF-8"?>
        <document name="http://blah">
          <price>111 USD</price>
          <color>red</color>
          <size>comfort</size>
          <cost>00012-40,00012-40,00012-40,00012-40</cost>
          <shipping>US::Ground:0.00,US::Ground:0.00,US::Ground:0.00</shipping>
        </document>
    <document name="http://blahblah">
          <price>110 USD</price>
          <color>blue</color>
          <size>super</size>
          <cost>00012-41,00012-41,00012-41,00012-41</cost>
          <shipping>US::Ground:0.01,US::Ground:0.01,US::Ground:0.01</shipping>
        </document>

This is the xslt used to create multi valued attributes( thanks to michael.hor257k). It is giving me duplicate values of the other attributes -

<?xml version="1.0" encoding="UTF-8"?>
        <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="@*"/>
              <xsl:copy-of select="@*|price"/>
              <xsl:copy-of select="@*|color"/>
              <xsl:copy-of select="@*|size"/>

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

How can I modify my XSLT 1.0 to strip the duplicate attributes, as well as keeping selected attributes as multi-valued? I have multiple document element so it should work for all the document elements.

标签: xml xslt
1条回答
祖国的老花朵
2楼-- · 2019-08-10 02:31

Please take your time and try to understand the comments and the mechanics behind how this works. Ask in the comments if you have any further questions.

<?xml version="1.0" encoding="UTF-8"?>
<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:key name="element-by-name" match="document/*" use="local-name()"/>


  <!-- Copy everything, unless other templates say otherwise -->
  <xsl:template match="*|@*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>


  <!-- Skip elements if there are elements of the same name earlier in the document. 
       Priority is set to 1 so that this template overrides other templates
       and we don't accidentally still get duplicates. -->
  <xsl:template match="*[count(. | key('element-by-name', local-name())[1]) > 1]" priority="1"/>


  <!-- This template is for elements we want to be multi-value -->
  <xsl:template match="shipping | cost">
    <xsl:copy>
      <!-- We step through all elements of the same name -->
      <xsl:for-each select="key('element-by-name', local-name())">
        <xsl:value-of select="."/>
        <xsl:if test="position() != last()">,</xsl:if>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
查看更多
登录 后发表回答