我正在阅读 XSLT 3.0 的 W3C 文档 here .我想知道如何使用这些元素:
(1) xsl:decimal-format
(2) xsl:stream
(3) xsl:accumulator
(4) xsl:accumulator-rule
(5) xsl:fork
显然,这些是一些较少使用的元素。由于此处给出的示例有限,此处提供的答案将使 future 的 XSLT 学习者受益。有人可以演示如何使用它们吗?
我知道要回答的问题很多。所以我会给任何正确的部分答案投票,希望它对其他人有用。
最佳答案
xsl:decimal-format
从 1.0 版开始就在规范中;您引用的其他元素是 3.0 版中的新元素,并且都与流相关联(即“即时”处理源文档的能力,而无需将整个树加载到内存中)。
<xsl:stream href="in.xml">
...do something...
</xsl:stream>
与
的效果基本相同<xsl:for-each select="doc('in.xml')">
...do something...
</xsl:for-each>
除了“做某事”是流式传输的(这意味着它必须符合流式传输规则)。例如,如果你想找出大量员工的平均工资,你可以这样做
<xsl:stream href="in.xml">
<result><xsl:value-of select="avg(//employee/@salary)"/></result>
</xsl:stream>
如果您想在输入文档的单次流处理过程中计算最低和最高薪水怎么办? xsl:fork 和 accumulators 都提供了解决这个问题的方法。 xsl:fork 允许您指定在同一遍期间发生的两个或多个计算,有效地并行:
<xsl:stream href="in.xml">
<result>
<xsl:fork>
<xsl:sequence>
<min><xsl:value-of select="min(//employee/@salary)"/></min>
</xsl:sequence>
<xsl:sequence>
<max><xsl:value-of select="max(//employee/@salary)"/></max>
</xsl:sequence>
</xsl:fork>
</result>
</xsl:stream>
xsl:accumulator 允许您定义作为阅读文档的副作用有效发生的处理:
<xsl:accumulator name="min-salary" initial-value="10000000">
<xsl:accumulator-rule match="employee"
select="if (@salary lt $value) then @salary else $value"/>
</xsl:accumulator>
然后您可以在处理过程中的任何时候通过调用 accumulator-before('min-salary')
读取到目前为止的最低工资。
关于xml - 如何使用xsl :stream, xsl :accumulator, xs :fork from XSLT 3. 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25832164/