gpt4 book ai didi

具有复杂条件的 XSLT for-each

转载 作者:行者123 更新时间:2023-12-04 06:09:54 24 4
gpt4 key购买 nike

我正在转换以下 XML 以生成 HTML。

XML



<条款代码="section6">

<变量 col="2"name="R1C2"row="1">真
气体

汽油
<变量 col="2"name="R3C2"row="3">真
<条款>

XSLT



1: 
    
     
2:
3:
4:
5:
6:
7:
8:
9:

转换工作正常并显示结果 1) 水 3) 汽油

问题是序列号。您可以在第 5 行中看到条件筛选行,这些行在 col 2 和用于显示序列号的 position() 中只有 'true' 值。我不能在 XLST 中运行计数器。

我想知道我是否可以在第 1 行使用 for-each 添加第 5 行的条件。上面示例的结果应该是 1) Water 2) Patrol 有什么建议吗?

最佳答案

这是否符合您的要求?

我在第 2 列为真时驱动 for-each 选择。这样的位置,等于我们在选定节点集中的位置,将等于 2 而不是 3

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="/">
<xsl:for-each select="clause/variable[@col='2' and text()='true']">
<xsl:sort select="@row" data-type="number"/>
<xsl:variable name="row-id" select="@row"/>
<xsl:value-of select="concat(position(), ') ')"/>
<xsl:value-of select="/clause/variable[@col='1' and @row=$row-id]"/>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

虽然我可能会优先使用模板而不是 for-each 并使用 current() 这样我们就不需要 row-id 变量:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="/">
<xsl:apply-templates select="clause/variable[@col='2' and text()='true']">
<xsl:sort select="@row" data-type="number"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="clause/variable[@col='2' and text()='true']">
<xsl:value-of select="concat(position(), ') ')"/>
<xsl:value-of select="/clause/variable[@col='1' and @row=current()/@row]"/>
</xsl:template>

</xsl:stylesheet>

关于具有复杂条件的 XSLT for-each,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9918456/

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