输出字符串操作的XSLT错误

时间:2012-03-28 10:37:12

标签: xslt

我创建了一个XSLT,它在以下输入中查找月份标记并拆分字符串并为每个字符串创建新标记。然而,我遇到的问题是它只查看第一个月的标记,并且对于任何子项<set>标记,它会使用第一个集合中的结果自动填充它们。

所以对于这个输入:

<?xml version="1.0" encoding="UTF8"?>
 <Response xmlns="http://www.castiron.com/response">
     <payload>
         <sets>
             <month>JUN,JUL</month>
             <season>Season11</season>
             <productId>1111111</productId>
         </sets>
         <sets>
            <month>AUG,SEP</month>
            <season>Season12</season>
            <productId>2222222</productId>
        </sets> 
    </payload>
</Response>

应该生成:

<?xml version="1.0" encoding="utf-8"?>
<Response xmlns="http://www.castiron.com/response">
    <payload xmlns:r="http://www.castiron.com/response">
        <sets>
            <month>JUN</month>
            <season>Season11</season>
            <productId>1111111</productId>
        </sets>
        <sets>
            <month>JUL</month>
            <season>Season11</season>
            <productId>1111111</productId>
        </sets>
        <sets>
            <month>AUG</month>
            <season>Season12</season>
            <productId>2222222</productId>
        </sets>
        <sets>
            <month>SEP</month>
            <season>Season12</season>
            <productId>2222222</productId>
        </sets>
    </payload>
</Response>

ACTUAL 响应是:

<?xml version="1.0" encoding="utf-8"?>
<Response xmlns="http://www.castiron.com/response">
    <payload xmlns:r="http://www.castiron.com/response">
        <sets>
            <month>JUN</month>
            <season>Season11</season>
            <productId>1111111</productId>
        </sets>
        <sets>
            <month>JUN</month>
            <season>Season12</season>
            <productId>2222222</productId>
        </sets>
        <sets>
            <month>JUL</month>
            <season>Season11</season>
            <productId>1111111</productId>
        </sets>
        <sets>
            <month>JUL</month>
            <season>Season12</season>
            <productId>2222222</productId>
        </sets>
    </payload>
</Response>

当前的XSLT是:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:r="http://www.castiron.com/response" exclude-result-prefixes="r">

    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:param name="month"/>

        <xsl:copy>
            <xsl:apply-templates select="@* | node()">
                <xsl:with-param name="month" select="$month"/>
            </xsl:apply-templates>
        </xsl:copy>

    </xsl:template>

    <xsl:template match="r:month">
        <xsl:param name="month"/>
        <month xmlns="http://www.castiron.com/response">
            <xsl:choose>
                <xsl:when test="$month">
                    <xsl:value-of select="$month"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates/>
                </xsl:otherwise>
            </xsl:choose>
        </month>
    </xsl:template>

    <xsl:template name="splitMonths">
        <xsl:param name="months"/>
        <xsl:variable name="firstMonth" select="substring-before($months,',')"/>
        <xsl:variable name="month">
            <xsl:choose>
                <xsl:when test="$firstMonth">
                    <xsl:value-of select="$firstMonth"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$months"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="otherMonths" select="substring-after($months,',')"/>
        <xsl:if test="$month">
            <xsl:apply-templates>
                <xsl:with-param name="month" select="$month"/>
            </xsl:apply-templates>
        </xsl:if>
        <xsl:if test="$otherMonths">
            <xsl:call-template name="splitMonths">
                <xsl:with-param name="months" select="$otherMonths"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template match="r:payload">

        <payload xmlns="http://www.castiron.com/response">

            <xsl:call-template name="splitMonths">
                <xsl:with-param name="months" select="r:sets/r:month"/>
            </xsl:call-template>

        </payload>
    </xsl:template>

</xsl:stylesheet>

任何人都可以提供帮助,因为我已经在这个人周围绞尽脑汁了好几天!

更新

我正在考虑循环浏览每个<sets>代码的想法,因此在上面的填充代码中使用此部分:

<xsl:template match="r:payload">
    <payload xmlns="http://www.castiron.com/response">
        <xsl:for-each select="r:sets">

        <xsl:call-template name="splitMonths">
            <xsl:with-param name="months" select="r:sets/r:month"/> 
        </xsl:call-template>

        </xsl:for-each>
    </payload>
</xsl:template>

然而,它产生以下输出:

<?xml version="1.0" encoding="utf-8"?>
<Response xmlns="http://www.castiron.com/response">
    <payload xmlns:r="http://www.castiron.com/response">
        <month/>
        <season>SS11</season>
        <productId>3600596</productId>
        <month/>
        <season>AW12</season>
        <productId>7001258</productId>
    </payload>
</Response>

2 个答案:

答案 0 :(得分:2)

这是一个不需要任何扩展的纯XSLT 1.0解决方案:

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.castiron.com/response"
  xmlns:r="http://www.castiron.com/response"
  exclude-result-prefixes="r">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="r:sets">
    <xsl:apply-templates select="r:month"/>
  </xsl:template>

  <xsl:template match="r:month">
    <xsl:param name="text" select="."/>
    <xsl:choose>
      <xsl:when test="not(contains($text, ','))">
        <sets>
          <month>
            <xsl:value-of select="$text"/>
          </month>
          <xsl:apply-templates select="../*[not(self::r:month)]"/>
        </sets>
      </xsl:when>
      <xsl:otherwise>
        <sets>
          <month>
            <xsl:value-of select="substring-before($text, ',')"/>
          </month>
          <xsl:apply-templates select="../*[not(self::r:month)]"/>
        </sets>
        <xsl:apply-templates select=".">
          <xsl:with-param name="text" select="substring-after($text, ',')"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

使用XSLT 2.0,它更容易:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.castiron.com/response"
  xmlns:r="http://www.castiron.com/response"
  exclude-result-prefixes="r">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="r:sets">
    <xsl:apply-templates select="r:month"/>
  </xsl:template>

  <xsl:template match="r:month">
    <xsl:param name="names" select="tokenize(., ',')"/>
    <xsl:choose>
      <xsl:when test="not($names[2])">
        <sets>
          <month>
            <xsl:value-of select="$names[1]"/>
          </month>
          <xsl:apply-templates select="../*[not(self::r:month)]"/>
        </sets>
      </xsl:when>
      <xsl:otherwise>
        <sets>
          <month>
            <xsl:value-of select="$names[1]"/>
          </month>
          <xsl:apply-templates select="../*[not(self::r:month)]"/>
        </sets>
        <xsl:apply-templates select=".">
          <xsl:with-param name="names" select="$names[position() gt 1]"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

[edit] XSLT 2.0解决方案是在之前的XSLT 1.0解决方案之后建模的,但我认为以下方法更紧凑,更易于理解和执行:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.castiron.com/response"
  xmlns:r="http://www.castiron.com/response"
  exclude-result-prefixes="r">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="r:sets">
    <xsl:variable name="this-set" select="."/>
    <xsl:for-each select="tokenize(r:month, ',')">
      <sets>
        <month>
          <xsl:value-of select="."/>
        </month>
        <xsl:apply-templates select="$this-set/*[not(self::r:month)]"/>
      </sets>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:1)

如何使用标记化器和显式匹配,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:r="http://www.castiron.com/response"
  xmlns:s="http://exslt.org/strings"
  exclude-result-prefixes="r">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="*|r:*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="r:sets">
    <xsl:variable name="months" select="s:tokenize(r:month, ',')"/>
    <xsl:variable name="this" select="*[name() != 'month']"/>
    <xsl:for-each select="$months">
      <xsl:element name="sets" namespace="http://www.castiron.com/response">
        <xsl:element name="month" namespace="http://www.castiron.com/response">
          <xsl:value-of select="."/>
        </xsl:element>
        <!-- all the other children -->
        <xsl:copy-of select="$this"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>