2012-03-27 59 views
2

我有,看起来像这样的XSLT:命名空间停止XSLT工作

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <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="month"> 
     <xsl:param name="month"/> 
     <month> 
      <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="payload"> 
     <payload> 
      <xsl:call-template name="splitMonths"> 
       <xsl:with-param name="months" select="sets/month"/> 
      </xsl:call-template> 
     </payload> 
    </xsl:template> 

</xsl:stylesheet> 

这是输入:

<?xml version="1.0" encoding="UTF8"?> 
<Response xmlns="http://www.castiron.com/response"> 
    <code>0</code> 
    <message>Success</message> 
    <payload> 
     <sets> 
      <month>AUG,SEP,OCT,NOV,DEC,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC,JAN</month> 
      <season>Season11</season> 
      <productId>11111</productId> 
     </sets> 
    </payload> 
</Response> 

因为在<Response xmlns="http://www.castiron.com/response">标签这个名称空间是造成整个XSLT也失败了。是否有可能消除命名空间,以便XSLT能够正常工作。如果你删除命名空间并运行XSLT,它就可以完美运行!

回答

3

在XSLT定义namesapce,即:

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

对不起,这是我的错误....他们应该是。它现在已经得到纠正。 – MMKD 2012-03-27 09:31:35

+0

@Keevil,我已经更新了我的答案。 – 2012-03-27 09:44:06

2

这是因为规则match="month"match="{http://www.castiron.com/response}month"不同。它们具有相同的节点名称但由于它们位于不同的命名空间中,因此它们被认为是不同的节点