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
Xslt 位置()!=最后一个()不工作_Xslt - Fatal编程技术网

Xslt 位置()!=最后一个()不工作

Xslt 位置()!=最后一个()不工作,xslt,Xslt,我有一个类似于: <ast> <group> <Set> <location line="1" column="22" offset="22"/> <group> <Id value="foo"> <location line="1" column="31" offset="3

我有一个类似于:

<ast>
    <group>
        <Set>
            <location line="1" column="22" offset="22"/>
            <group>
                <Id value="foo">
                    <location line="1" column="31" offset="31"/>
                </Id>
            </group>
            <group>
                <Function>
                    <location line="1" column="22" offset="22"/>
                    <end-location line="1" column="49" offset="49"/>
                    <group>
                        <Id value="a">
                            <location line="1" column="35" offset="35"/>
                        </Id>
                        <Id value="b">
                            <location line="1" column="37" offset="37"/>
                        </Id>
                    </group>
                    <group>
                        <Return>
                            <location line="1" column="40" offset="40"/>
                            <Number value="0">
                                <location line="1" column="47" offset="47"/>
                            </Number>
                        </Return>
                    </group>
                </Function>
            </group>
        </Set>
        ...
    </group>
</ast>
虽然它应该是:

<?xml version="1.0"?>
<function-def name="foo(a,b)">...

...

它正在工作,但不是以您认为的方式

在第一个模板中,有一个
xsl:apply-templates

<xsl:apply-templates select="group[position()=2]/Function" />
这将选择同样没有模板的
元素。现在,当它执行
操作时,将选择所有子节点,其中包括用于缩进XML的空文本节点

问题是,当您测试
position()=last()
时,您正在测试元素在已选择的所有子节点集中的位置,其中包括文本节点。最后一个
id
之后有一个空文本节点,因此
id
可能是最后一个
id
元素,但它不是最后一个子节点

一种解决方案是告诉XSLT去掉空文本节点,这样
id
就会成为最后一个子节点

 <xsl:strip-space elements="*" />

它在起作用,但不是以你认为的方式

在第一个模板中,有一个
xsl:apply-templates

<xsl:apply-templates select="group[position()=2]/Function" />
这将选择同样没有模板的
元素。现在,当它执行
操作时,将选择所有子节点,其中包括用于缩进XML的空文本节点

问题是,当您测试
position()=last()
时,您正在测试元素在已选择的所有子节点集中的位置,其中包括文本节点。最后一个
id
之后有一个空文本节点,因此
id
可能是最后一个
id
元素,但它不是最后一个子节点

一种解决方案是告诉XSLT去掉空文本节点,这样
id
就会成为最后一个子节点

 <xsl:strip-space elements="*" />

哦我认为带有
match=“Function/group[position()=1]/Id”
的模板将匹配
调用…
是只选择子节点的
的缩写。因此,内置模板将在后台为您进行处理。谢谢,
正是我所需要的!我认为带有
match=“Function/group[position()=1]/Id”
的模板将匹配
调用…
是只选择子节点的
的缩写。因此,内置模板将在后台为您进行处理。谢谢,
正是我所需要的
 <xsl:strip-space elements="*" />
<xsl:template match="Function/group[position()=1]">
    <xsl:apply-templates select="Id" />
</xsl:template>