我已经创建了一个XSLT,它在以下输入中查找月份标签,并拆分字符串并为每个字符串创建新标签。然而,我遇到的问题是,它只查看第一个月的标签,并且任何子标签<set>
标签会自动填充第一组的结果。带输出字符串操作的XSLT错误
因此,对于这个输入:
<?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>
它SHOULD产品:
<?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>
但是其实际回应是:
<?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>
任何人都可以帮忙,因为我一直在围绕这一天的大脑围绕着我!
UPDATE
我看通过每个<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>
当我在氧气中运行时,出现以下错误'URL http://exslt.org/strings没有标识外部Java类',然后突出显示此行' ' –
MMKD
2012-03-28 11:10:46
我不知道氧气是什么,但它是一个xslt扩展名,并且有源代码,请参阅http:// www。 exslt.org/str/index.html – hroptatyr 2012-03-28 11:13:03
也许它适用于'extension-element-prefixes =“s”',因为他们提示 – hroptatyr 2012-03-28 11:13:40