虽然这是在XSLT 2.0直线前进,在XSLT两遍转化可以产生想要的结果:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kSolByVal" match="solution" use="."/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:apply-templates/>
</xsl:variable>
<xsl:apply-templates select=
"ext:node-set($vrtfPass1)
/*/*/*/*
/solutions/solution
[generate-id()
=
generate-id(key('kSolByVal', .)[1])
]"
mode="pass2"/>
</xsl:template>
<xsl:template mode="pass2" match="solution">
<div id="#{.}">
<xsl:apply-templates mode="pass2"
select="key('kSolByVal', .)/../../../.."/>
</div>
</xsl:template>
<xsl:template match="profile" mode="pass2">
<xsl:if test="position() = 1">
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:value-of select=
"concat(customer, ', ', */summary, '
')"/>
</xsl:template>
<xsl:template match="solutions">
<solutions>
<xsl:apply-templates select="." mode="split"/>
</solutions>
</xsl:template>
<xsl:template match="solutions" name="split" mode="split">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText)">
<xsl:variable name="vText1"
select="concat($pText, ',')"/>
<xsl:variable name="vPart" select=
"substring-before($vText1, ',')"/>
<solution>
<xsl:value-of select="$vPart"/>
</solution>
<xsl:call-template name="split">
<xsl:with-param name="pText"
select="substring($pText, string-length($vPart)+2)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
当这个变换所提供的XML文档施加(校正良构):
<profiles>
<profile>
<customer>customer a </customer>
<collateral>
<summary>summary a</summary>
<related>
<solutions>sol1,sol2</solutions>
</related>
</collateral>
</profile>
<profile>
<customer>customer b </customer>
<collateral>
<summary>summary b</summary>
<related>
<solutions>sol1</solutions>
</related>
</collateral>
</profile>
<profile>
<customer>customer c </customer>
<collateral>
<summary>summary c</summary>
<related>
<solutions>sol2,sol3</solutions>
</related>
</collateral>
</profile>
</profiles>
有用,正确的结果产生:
<div id="#sol1">
customer a , summary a
customer b , summary b
</div>
<div id="#sol2">
customer a , summary a
customer c , summary c
</div>
<div id="#sol3">
customer c , summary c
</div>
说明:
我们开展两次传球转化。 Pass2应用于在提供的XML文档上应用Pass1的结果。
通过1本质上是任何solutions
元素的身份规则覆盖。对solutions
元素的处理包括递归分割其字符串值。 1步的最终结果是:
-
<profiles>
<profile>
<customer>customer a </customer>
<collateral>
<summary>summary a</summary>
<related>
<solutions>
<solution>sol1</solution>
<solution>sol2</solution>
</solutions>
</related>
</collateral>
</profile>
<profile>
<customer>customer b </customer>
<collateral>
<summary>summary b</summary>
<related>
<solutions>
<solution>sol1</solution>
</solutions>
</related>
</collateral>
</profile>
<profile>
<customer>customer c </customer>
<collateral>
<summary>summary c</summary>
<related>
<solutions>
<solution>sol2</solution>
<solution>sol3</solution>
</solutions>
</related>
</collateral>
</profile>
</profiles>
0.3。然后,我们对Pass1的结果应用模板(在mode="pass2"
中)。这是一个典型和传统的Muenchian分组。
谢谢你的帮助 – gechu
@gechu:不客气。 –
另外,解释的解释。如果你不介意。为什么我们需要将此分配给变量 并且还需要ext:node-set?再次感谢 –
gechu