gpt4 book ai didi

xml - xsl :for-each loop counter

转载 作者:数据小太阳 更新时间:2023-10-29 01:42:23 25 4
gpt4 key购买 nike

如何保存在 xsl:for-each 中发生的迭代? (XSL 中的变量是不可变的)

我的目标是找到特定级别的任何节点的最大子节点数。

例如,我可能想打印本次调查中任何问题的响应节点不超过 2 个:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="testing.xsl"?>
<Survey>
<Question>
<Response text="Website" />
<Response text="Print Ad" />
</Question>
<Question>
<Response text="Yes" />
</Question>
</Survey>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:for-each select="Survey">
The survey has <xsl:value-of select="count(child::Question)"/> questions.
<br />
<xsl:variable name="counter">0</xsl:variable>
<xsl:for-each select="Question">
<!-- TODO: increment the counter ??????? -->
</xsl:for-each>
No more than <xsl:value-of select="$counter"/> responses were returned for any question.
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

最佳答案

不会“保存在 xsl:for-each 中发生的迭代”,因为 XSLT is a functional language 和变量是不可变的。

以下转换找到所需的最大值:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>    <xsl:template match="/">      <xsl:call-template name="maximum">        <xsl:with-param name="pNodes" select="*/Question"/>      </xsl:call-template>    </xsl:template>    <xsl:template name="maximum">      <xsl:param name="pNodes"/>      <xsl:variable name="vNumNodes" select="count($pNodes)"/>      <xsl:choose>        <xsl:when test="$vNumNodes = 1">          <xsl:value-of select="count($pNodes[1]/Response)"/>        </xsl:when>        <xsl:otherwise>          <xsl:variable name="vHalf"                select="floor($vNumNodes div 2)"/>          <xsl:variable name="vMax1">           <xsl:call-template name="maximum">            <xsl:with-param name="pNodes"                 select="$pNodes[not(position() > $vHalf)]"/>           </xsl:call-template>          </xsl:variable>          <xsl:variable name="vMax2">           <xsl:call-template name="maximum">            <xsl:with-param name="pNodes"                 select="$pNodes[position() > $vHalf]"/>           </xsl:call-template>          </xsl:variable>          <xsl:value-of select=           "$vMax1*($vMax1 >= $vMax2) + $vMax2*($vMax2 > $vMax1)"/>        </xsl:otherwise>      </xsl:choose>    </xsl:template></xsl:stylesheet>

应用于提供的 XML 文档时:

<Survey>    <Question>        <Response text="Website" />        <Response text="Print Ad" />    </Question>    <Question>        <Response text="Yes" />    </Question></Survey>

产生了想要的结果:

2

请注意以下几点:名为maximum”的模板递归调用自身并实现 DVC (Divide and Conquer principle) < em>最小化递归堆栈深度。节点列表被分成两部分,计算(递归)两个列表的最大值并返回两者中较大的一个。

关于xml - xsl :for-each loop counter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/339110/

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