Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有复杂条件的每种类型的XSLT_Xslt_Foreach - Fatal编程技术网

具有复杂条件的每种类型的XSLT

具有复杂条件的每种类型的XSLT,xslt,foreach,Xslt,Foreach,我正在转换以下XML以生成HTML XML 水 真的 煤气 汽油 真的 XSLT 1: 2: 三: 4: 5: 6: 7: 8: 9: 改造效果良好,结果显示1)水3)汽油 问题是序列号。您可以在第5行的过滤器行上看到条件,这些行在第2列中只有“true”值,位置()用于显示序列号。我不能在XLST中使用运行计数器 我想知道是否可以为第1行的每一行添加第5行的条件。上面例子的结果应该是1)水2)巡逻有什么建议吗?这是你想要的吗 我认

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

XML


水
真的
煤气
汽油
真的

XSLT

1:
2:   
三:
4:   
5:   
6:      
7:      
8:   
9: 
改造效果良好,结果显示1)水3)汽油

问题是序列号。您可以在第5行的过滤器行上看到条件,这些行在第2列中只有“true”值,位置()用于显示序列号。我不能在XLST中使用运行计数器


我想知道是否可以为第1行的每一行添加第5行的条件。上面例子的结果应该是1)水2)巡逻有什么建议吗?

这是你想要的吗

我认为col 2上的每个选择都是正确的。这样,位置(等于我们在所选节点集中的位置)将等于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>

我认为不能用一个XPath 1.0表达式来表达这一点

在XSLT 1.0中,我将使用键,解决方案将变得简短、优雅和高效

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:key name="kVarCol2" match="variable[@col=2]" use="@row"/>

 <xsl:template match="/*">
    <xsl:for-each select="variable[@col='1'][key('kVarCol2', @row)='true']">
     <xsl:sort select="@row" data-type="number"/>
        <xsl:value-of select="concat('&#xA;', position(), ') ', .)"/>
    </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
1) Water
2) Petrol
1) Water 
2) Petrol

II。XPath 2.0(单表达式)解决方案:

<clause code="section6">
    <variable col="1" name="R1C1" row="1">Water</variable>
    <variable col="2" name="R1C2" row="1">true</variable>
    <variable col="1" name="R2C1" row="2">Gas</variable>
    <variable col="2" name="R2C2" row="2"></variable>
    <variable col="1" name="R3C1" row="3">Petrol</variable>
    <variable col="2" name="R3C2" row="3">true</variable>
</clause>
for $i in 1 to max(/*/*/@row/xs:integer(.))
  return
       /*/variable[@row eq string($i)]
            [@col eq '1'
           and
             ../variable
                 [@row eq string($i)
                and
                  @col eq '2'
                and
                  . eq 'true'
                 ]
            ]
             /concat('&#xA;', position(), ') ', .)
在同一XML文档(如上)上计算此XPath 2.0表达式时,结果是相同的所需字符串

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:key name="kVarCol2" match="variable[@col=2]" use="@row"/>

 <xsl:template match="/*">
    <xsl:for-each select="variable[@col='1'][key('kVarCol2', @row)='true']">
     <xsl:sort select="@row" data-type="number"/>
        <xsl:value-of select="concat('&#xA;', position(), ') ', .)"/>
    </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
1) Water
2) Petrol
1) Water 
2) Petrol

这是一个很好的问题,令人惊讶的是,它有一个解决方案,即一个XPath2.0表达式。在XSLT1.0中,可以使用键实现简短、优雅和高效的解决方案+1.我仍然不了解XSLT2。我维护的代码库都使用XSLT1,即使我们使用的是Saxon 9。我还没有找到一个好的资源,上面说在1中你曾经做过这个-在2中不要这样做那样-你知道吗?@Kevan:我记得最近xslt标签中有这样一个问题,Michael Kay和我都回答了。