position()= 1正常工作,但不是position()<5 [英] position()=1 working correctly, but not position()&lt;5

查看:366
本文介绍了position()= 1正常工作,但不是position()<5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是XSLT新手,正在使用w3schools自己动手进行一些测试。我正在使用下面的演示:



http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=tryxsl_choose



这包含以下行:

 < xsl:for-each select =catalog / cd> 

我正在测试过滤由 position()但我在使用< 操作数时遇到问题。



$

 < xsl:for-each select =catalog / cd  [位置()= 1]> 

这将返回XML数据中的第一项(如预期的那样)。
$ b




然后我试过:

 < xsl:for-each select =catalog / cd [position()< 5]> 

我期望这个返回前四个项目,但是我没有得到任何结果。 >

我的猜测是,也许 position()= 1 正在进行字符串比较,这就是为什么它返回第一个项目,但它不能理解 position()<5 作为一个字符串不能用这种方式进行比较?



为什么是这发生了什么,什么是正确的语法来获得我希望实现的结果?






更新: 阅读@ joocer的回应后,自己测试一下,使用> 操作数确实有效,结果相反:

 < xsl:for-each select =catalog / cd [(position()> 5)]> 


解决方案

c>< 作为& lt; ,所以它不会被混淆为元素标记的开始。我认为这样做是为了让容错解析器更容易从错误中恢复,流解析器更快地跳过内容。他们总是可以在 CDATA 之外查找< ,并知道这是一个元素开始或结束标记。


I'm new to XSLT, and I'm carrying out a few tests using w3schools "Try it yourself" pages. I'm using the following demo:

http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=tryxsl_choose

This contains the following line:

<xsl:for-each select="catalog/cd">

I'm testing filtering the HTML rendered by position() but I'm having issues when using the < operand.


I've tried the following:

<xsl:for-each select="catalog/cd[position()=1]">

And this returns the first item from the XML data (as expected).


I then tried:

<xsl:for-each select="catalog/cd[position()<5]">

I was expecting this to return the first 4 items, but instead I get no results.

My guess is that perhaps position()=1 is doing a string comparison, which is why it returns the first item, but it cannot understand position()<5 as a string cannot be compared in this way?

Why is this happening, and what would be the correct syntax to get the results I wish to achieve?


Update: After reading @joocer's response, and testing this myself, using the > operand does work, for the opposite result:

<xsl:for-each select="catalog/cd[(position()>5)]">

解决方案

Even inside quotes, you must type < as &lt; so it won't be confused for the start of an element tag. I think this was done to make it easier for tolerant parsers to recover from errors and streaming parsers skip content faster. They can always look for < outside CDATA and know that is an element start or end tag.

这篇关于position()= 1正常工作,但不是position()<5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆