我对这个 XSL 问题感到抓狂!
问题是我想根据表格中选择的内容对报纸进行排序。如果$sort_newspaper = 'name'
它应该在名称( <xsl:sort select="./@name"/>
)之后排序...但是...如果 xsl:sort
则不起作用存在于选择之内或之后。它也不适用于 xsl:if
.
要明确的是,它的工作方式就像现在的代码一样,选择工作......
<xsl:for-each select="./newspaper[count(. | key('newspaper_key', ./@id)[1]) = 1]">
<xsl:sort select="./@name"/>
<xsl:choose>
<xsl:when test="$sort_newspaper = 'name'">
XSL:SORT SHOULD BE HERE BUT THAT WILL RESULT IN ERROR!
</xsl:when>
<xsl:otherwise>
HALLO
</xsl:otherwise>
</xsl:choose>
IF XSL:SORT WOULD BE HERE IT WOULD RESULT IN ERROR TOO!
</xsl:for-each>
最佳答案
抱歉,有坏消息。这是行不通的。唯一可能的解决方案(我目前看到)是将整个 xsl:for-each
放入 xsl:when
(带或不带排序)。
您的代码示例应该如下所示:
<xsl:variable name="newspaper_group" select="./newspaper[count(. | key('newspaper_key', ./@id)[1]) = 1]" />
<xsl:choose>
<xsl:when test="$sort_newspaper = 'name'">
<xsl:for-each select="$newspaper_group" >
<xsl:sort select="./@name"/>
<!-- Sorted stuff -->
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$newspaper_group" >
<!-- Unsorted stuff -->
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
关于xml - xsl :sort does not work together with xsl:choose or if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16695538/