gpt4 book ai didi

xslt - XSLT 1.0 中没有扩展功能的备用排序节点

转载 作者:行者123 更新时间:2023-12-04 15:15:04 26 4
gpt4 key购买 nike

这是一个与 XSL: Transforming xml into a sorted multicolumn html table 非常相似的问题

但是(不幸的是)有一个额外的要求:它应该是没有扩展功能的 XSLT 1.0,即不使用节点集功能。

这是我的简化 XML:

<demo>
<config n_columns="3" />
<messages>
<msg date="2011-07-06" title="2nd message" />
<title>message list</title>
<msg date="2011-07-05" title="4th message" />
<msg date="2011-07-06" title="3rd message" />
<msg date="2011-07-07" title="1st message" />
</messages>
</demo>

使用这个样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<xsl:apply-templates select="demo/messages">
<xsl:with-param name="n_columns" select="number(/demo/config/@n_columns)" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="messages">
<xsl:param name="n_columns" />
<div>
<xsl:value-of select="concat(./title, ' (', $n_columns, ' columns)')" />
</div>
<table>
<xsl:variable name="cells" select="msg" />
<xsl:apply-templates select="$cells[(position() - 1) mod $n_columns = 0]"
mode="row">
<xsl:with-param name="n_columns" select="$n_columns" />
<xsl:with-param name="cells" select="$cells" />
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match="msg" mode="row">
<xsl:param name="n_columns" />
<xsl:param name="cells" />
<xsl:variable name="n_row" select="position()" />
<xsl:variable name="row_cells"
select="$cells[position() > ($n_row - 1) * $n_columns][position() &lt;= $n_columns]" />
<tr>
<xsl:apply-templates select="$row_cells" mode="cell" />
<xsl:call-template name="empty-cells">
<xsl:with-param name="n" select="$n_columns - count($row_cells)" />
</xsl:call-template>
</tr>
</xsl:template>
<xsl:template match="msg" mode="cell">
<td>
<xsl:value-of select="@title" />
</td>
</xsl:template>
<xsl:template name="empty-cells">
<xsl:param name="n" />
<xsl:if test="$n > 0">
<td>
<xsl:attribute name="colspan">
<xsl:value-of select="$n" />
</xsl:attribute>
<xsl:text>&#xA0;</xsl:text>
</td>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

生成此 HTML 片段作为输出:
<div>message list (3 columns)</div>
<table>
<tr>
<td>2nd message</td>
<td>4th message</td>
<td>3rd message</td>
</tr>
<tr>
<td>1st message</td>
<td colspan="2">&nbsp;</td>
</tr>
</table>

显然缺少的是排序部分......

重新定义“cells”变量如下实际上是我需要的:
<xsl:variable name="cells">
<xsl:for-each select="msg">
<xsl:sort select="@date" order="descending" />
<xsl:sort select="@title" />
<xsl:copy-of select="." />
</xsl:for-each>
</xsl:variable>

但是现在我必须定义另一个变量以将 RTF 转换为节点列表并将该变量传递给我正在应用的模板。
<xsl:variable name="sCells" select="ext:node-set($cells)/*" />

这样做会产生以下 HTML 片段:
<div>message list (3 columns)</div>
<table>
<tr>
<td>1st message</td>
<td>2nd message</td>
<td>3rd message</td>
</tr>
<tr>
<td>4th message</td>
<td colspan="2">&nbsp;</td>
</tr>
</table>

不幸的是,我的 XSLT 引擎(用于 Java 的 SAP XML 工具包)不支持这个(或类似的)扩展功能。因此,我正在寻找另一种不需要节点集扩展功能的解决方案。

我花了相当多的时间阅读各种论坛等,但我真的想不通。也许有人对替代方法有一个好主意?天!

这是基于 Dimitre 的(稍微扩展的)解决方案的后续行动。
这个 XML 输入
<demo>
<config n_columns="3" />
<messages>
<msg date="2011-07-06" title="2nd message" />
<title>message list</title>
<msg date="2011-07-05" title="4th message" />
<msg date="2011-07-06" title="3rd message" />
<msg date="2011-07-07" title="1st message" />
<msg date="2011-07-05" title="5th message" />
<msg date="2011-07-05" title="7th message" />
<msg date="2011-07-05" title="6th message" />
</messages>
</demo>

结合这个 XSLT 样式表
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />

<xsl:variable name="vNumCols" select="/*/config/@n_columns" />
<xsl:variable name="vCells" select="/*/messages/msg" />
<xsl:variable name="vNumCells" select="count($vCells)" />
<xsl:variable name="vNumRows" select="ceiling($vNumCells div $vNumCols)" />
<xsl:variable name="vIndexPatternLength"
select="string-length(concat('', $vNumCells))" />
<xsl:variable name="vIndexPattern">
<xsl:call-template name="padding">
<xsl:with-param name="length" select="$vIndexPatternLength" />
<xsl:with-param name="chars" select="'0'" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vSortedIndex">
<xsl:for-each select="$vCells">
<xsl:sort select="@date" order="descending" />
<xsl:sort select="@title" />
<xsl:value-of
select="format-number(count(preceding-sibling::msg) + 1,
$vIndexPattern)" />
</xsl:for-each>
</xsl:variable>

<xsl:template match="/">
<xsl:apply-templates select="demo/messages" />
</xsl:template>
<xsl:template match="messages">
<table>
<xsl:for-each select="$vCells[not(position() > $vNumRows)]">
<xsl:variable name="vRow" select="position()" />
<tr>
<xsl:for-each select="$vCells[not(position() > $vNumCols)]">
<xsl:variable name="vCol" select="position()" />
<xsl:variable name="vCell"
select="($vRow - 1) * $vNumCols + $vCol" />
<xsl:variable name="vIndex"
select="substring($vSortedIndex,
($vCell - 1) * $vIndexPatternLength + 1,
$vIndexPatternLength)" />
<xsl:variable name="vMessage"
select="$vCells[position() = $vIndex]" />
<xsl:choose>
<xsl:when test="$vMessage">
<xsl:apply-templates select="$vMessage"
mode="cell" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="empty-cell" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>

<xsl:template match="msg" mode="cell">
<td>
<xsl:apply-templates select="." />
</td>
</xsl:template>
<xsl:template match="msg">
<xsl:value-of select="concat(@date, ' : ', @title)" />
</xsl:template>

<xsl:template name="empty-cell">
<td>
<xsl:text>&#xA0;</xsl:text>
</td>
</xsl:template>

<!-- http://www.exslt.org/str/functions/padding/ -->
<xsl:template name="padding">
<xsl:param name="length" select="0" />
<xsl:param name="chars" select="' '" />
<xsl:choose>
<xsl:when test="not($length) or not($chars)" />
<xsl:otherwise>
<xsl:variable name="string"
select="concat($chars, $chars, $chars, $chars, $chars,
$chars, $chars, $chars, $chars, $chars)" />
<xsl:choose>
<xsl:when test="string-length($string) >= $length">
<xsl:value-of select="substring($string, 1, $length)" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="padding">
<xsl:with-param name="length" select="$length" />
<xsl:with-param name="chars" select="$string" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

产生这个 HTML 输出
<table>
<tr>
<td>2011-07-07 : 1st message</td>
<td>2011-07-06 : 2nd message</td>
<td>2011-07-06 : 3rd message</td>
</tr>
<tr>
<td>2011-07-05 : 4th message</td>
<td>2011-07-05 : 5th message</td>
<td>2011-07-05 : 6th message</td>
</tr>
<tr>
<td>2011-07-05 : 7th message</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>

谢谢迪米特!

最佳答案

在不使用任何扩展函数的情况下,在 XSLT 1.0 中执行所需的处理是困难的,但并非不可能 :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="vNumCols"
select="/*/config/@n_columns"/>

<xsl:variable name="vItems" select="/*/messages/msg"/>

<xsl:variable name="vNumItems" select="count($vItems)"/>

<xsl:variable name="vNumRows" select=
"ceiling($vNumItems div $vNumCols)"/>

<xsl:variable name="vsortedInds">
<xsl:for-each select="$vItems">
<xsl:sort select="@date" order="descending"/>

<xsl:value-of select=
"format-number(count(preceding-sibling::msg)+1,
'0000'
)
"/>
</xsl:for-each>
</xsl:variable>

<xsl:template match="/">
<table>
<xsl:for-each select=
"$vItems[not(position() > $vNumRows)]">
<tr>
<xsl:variable name="vRow" select="position()"/>

<xsl:for-each select="$vItems[not(position() > $vNumCols)]">
<xsl:variable name="vcurIndIndex" select=
"($vRow -1)*$vNumCols + position()"/>
<xsl:variable name="vcurInd" select=
"substring($vsortedInds, 4*($vcurIndIndex -1) +1, 4)"/>

<xsl:variable name="vcurItem" select="$vItems[position()=$vcurInd]"/>
<xsl:if test="$vcurItem">
<td>
<xsl:value-of select="$vcurItem/@title"/>
</td>
</xsl:if>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时 :
<demo>
<config n_columns="3" />
<messages>
<msg date="2011-07-06" title="2nd message" />
<title>message list</title>
<msg date="2011-07-05" title="4th message" />
<msg date="2011-07-06" title="3rd message" />
<msg date="2011-07-07" title="1st message" />
</messages>
</demo>

产生了所需输出的重要部分 (我将剩下的作为练习留给读者:)):
<table>
<tr>
<td>1st message</td>
<td>2nd message</td>
<td>3rd message</td>
</tr>
<tr>
<td>4th message</td>
</tr>
</table>

说明 :
  • 为了避免必须将 RTF 转换为节点集,我们使用了已排序元素的索引字符串 .每个索引占用四个字符(必要时用零填充左)。然后我们使用这些索引来填充表的行。
  • 为避免复活 ,我们正在使用 Piez method通过 N 个非节点项进行迭代。

  • 请注意 :此解决方案假定表格包含的单元格不超过 9999 个。如果需要更多单元格,您可以轻松更改代码,例如:

    代替:
    format-number(count(preceding-sibling::msg)+1,
    '0000'
    )

    和:
    format-number(count(preceding-sibling::msg)+1,
    '00000'
    )

    并替换:
              <xsl:variable name="vcurInd" select=
    "substring($vsortedInds, 4*($vcurIndIndex -1) +1, 4)"/>


              <xsl:variable name="vcurInd" select=
    "substring($vsortedInds, 5*($vcurIndIndex -1) +1, 5)"/>

    关于xslt - XSLT 1.0 中没有扩展功能的备用排序节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6596256/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com