XSLT格式输出完成90%

时间:2012-03-19 13:36:44

标签: xml xslt xpath

我正在尝试处理一些XML来改进它以便输入,但是我得到的问题可以在下面更好地说明:

INPUT

<?xml version="1.0" encoding="UTF8"?>
 <doc:ZINITIALIZE_FILTERS.Response xmlns:doc="urn:sapcom:document:sap:rfc:functions">
    <MESSAGE>Success</MESSAGE>
    <STATUS>0</STATUS>
     <COLLECTION>
         <item>
            <COLLECTION>A1</COLLECTION>
            <SEASON>S09</SEASON>
            <TEXT>Spring Market A1</TEXT>
        </item>
         <item>
            <COLLECTION>A1</COLLECTION>
            <SEASON>S10</SEASON>
            <TEXT>Spring Market A1</TEXT>
        </item>
     </COLLECTION>
</doc:ZINITIALIZE_FILTERS.Response>

期望的输出:

<?xml version="1.0" encoding="UTF8"?>
 <doc:ZINITIALIZE_FILTERS.Response xmlns:doc="urn:sapcom:document:sap:rfc:functions">
    <MESSAGE>Success</MESSAGE>
    <STATUS>0</STATUS>
     <COLLECTION>
         <item>
            <COLLECTION>A1</COLLECTION>
            <SEASON>S09,S10</SEASON>
            <TEXT>Spring Market A1</TEXT>
        </item>
     </COLLECTION>
</doc:ZINITIALIZE_FILTERS.Response>

正在使用XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" indent="yes"/>

    <xsl:key name="k" match="item" use="COLLECTION"/>

    <xsl:template match="/ZINITIALIZE_FILTERS.Response">
        <xsl:copy>
            <COLLECTIONS>
                <xsl:apply-templates select="COLLECTION/item[generate-id() = 
                    generate-id(key('k', COLLECTION))]"/>
            </COLLECTIONS>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="COLLECTION/item">
        <xsl:copy>
            <xsl:copy-of select="COLLECTION"/>
            <SEASON>
                <xsl:for-each select="key('k', COLLECTION)">
                    <xsl:value-of select="SEASON"/>

                    <xsl:if test="position() != last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </SEASON>
            <xsl:copy-of select="TEXT"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

当前不正确的输出:

<?xml version="1.0" encoding="UTF-8"?>
  <ZINITIALIZE_FILTERS.Response xmlns=""><?xml version="1.0"?>
    Success0<item xmlns:doc="urn:sap-com:document:sap:rfc:functions"><COLLECTION>A1</COLLECTION><SEASON>S09,S10,S12</SEASON><TEXT>Spring Market A1</TEXT></item>
  </ZINITIALIZE_FILTERS.Response>

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

使用:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="k" match="item" use="COLLECTION"/>

    <xsl:template match="item[generate-id() = generate-id(key('k', COLLECTION))]">
        <xsl:copy>
            <xsl:copy-of select="COLLECTION"/>
            <SEASON>
                <xsl:for-each select="key('k', COLLECTION)">
                    <xsl:value-of select="SEASON"/>

                    <xsl:if test="position() != last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </SEASON>
            <xsl:copy-of select="TEXT"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="item"/>

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

</xsl:stylesheet>

应用于此XML时:

<doc:ZINITIALIZE_FILTERS.Response xmlns:doc="urn:sapcom:document:sap:rfc:functions">
    <MESSAGE>Success</MESSAGE>
    <STATUS>0</STATUS>
    <COLLECTION>
        <item>
            <COLLECTION>A1</COLLECTION>
            <SEASON>S09</SEASON>
            <TEXT>Spring Market A1</TEXT>
        </item>
        <item>
            <COLLECTION>A1</COLLECTION>
            <SEASON>S10</SEASON>
            <TEXT>Spring Market A1</TEXT>
        </item>
    </COLLECTION>
</doc:ZINITIALIZE_FILTERS.Response>

它产生想要的正确结果:

<doc:ZINITIALIZE_FILTERS.Response xmlns:doc="urn:sapcom:document:sap:rfc:functions">
  <MESSAGE>Success</MESSAGE>
  <STATUS>0</STATUS>
  <COLLECTION>
    <item>
      <COLLECTION>A1</COLLECTION>
      <SEASON>S09,S10</SEASON>
      <TEXT>Spring Market A1</TEXT>
    </item>
  </COLLECTION>
</doc:ZINITIALIZE_FILTERS.Response>