Xml 为什么此比较仅适用于整数类型id?
我想知道只有当id为整数时,Xml 为什么此比较仅适用于整数类型id?,xml,xslt,xpath,Xml,Xslt,Xpath,我想知道只有当id为整数时,[@id=current()/@id]语句才能工作吗?例如: <elem id="1"> <elem id="1"> 我的错误输出:(它根本不合并) 约翰 Y 求爱 3. Y 2. 好啊 Y 99 不好 Y ... ... 预期产出: <root> <library id="L1"> <genre id="a"> <shelf1 id="1">
[@id=current()/@id]
语句才能工作吗?例如:
<elem id="1">
<elem id="1">
我的错误输出:(它根本不合并)
约翰
Y
求爱
3.
Y
2.
好啊
Y
99
不好
Y
...
...
预期产出:
<root>
<library id="L1">
<genre id="a">
<shelf1 id="1">
<book id="1a" action="borrow">
<attributes>
<user>Woo</user>
<length>2</length>
<condition>ok</condition>
</attributes>
<other1>y</other1>
</book>
<book id="2" action="extend">
<attributes>
<length>99</length>
<condition>not-ok</condition>
</attributes>
<other>y</other>
</book>
</shelf1>
<shelf1 id="b">...
</shelf1>
</genre>
<genre id="b">...
</genre>
</library>
</root>
求爱
2.
好啊
Y
99
不好
Y
...
...
对于每个操作为“借用”的节点,后跟一个或多个操作为“扩展”的节点
- 使用action=borrow将其合并到节点
- 将属性子项合并在一起,这样它将具有来自具有最新值的同级的所有唯一属性
- 让其他孩子保持不变
John字符串比较不需要显式字符串转换。换句话说,它应该像你写的那样工作。下面是一个例子:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="elem">
<xsl:if test="count(../elem[@id=current()/@id]) > 1">
<xsl:value-of select="@id"/>: has dup
</xsl:if>
</xsl:template>
</xsl:stylesheet>
发件人:
否则,如果至少有一个要比较的对象是数字,则
将要比较的每个对象转换为一个数字,就像通过应用
数字函数。否则,要比较的两个对象都是
通过应用字符串函数转换为字符串
也许你有一个上下文问题。比较应该有效。这里有一个例子 XML输入
<tests>
<test>
<elem id="1"/>
<elem id="1"/>
</test>
<test>
<elem id="AAA"/>
<elem id="AAA"/>
</test>
<test>
<elem id="BBB"/>
<elem id="CCC"/>
</test>
<test>
<elem id="2"/>
<elem id="3"/>
</test>
</tests>
Comparing id "1"
true
Comparing id "AAA"
true
Comparing id "BBB"
false
Comparing id "2"
false
编辑
<tests>
<test>
<elem id="1"/>
<elem id="1"/>
</test>
<test>
<elem id="AAA"/>
<elem id="AAA"/>
</test>
<test>
<elem id="BBB"/>
<elem id="CCC"/>
</test>
<test>
<elem id="2"/>
<elem id="3"/>
</test>
</tests>
Comparing id "1"
true
Comparing id "AAA"
true
Comparing id "BBB"
false
Comparing id "2"
false
在看了更新的示例之后,它看起来确实像是一个上下文问题。请看一下模板的开头:
<xsl:template match="genre/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="
book[@id=current()/@id][@action='extend']
[not( preceding-sibling::book[@id=current()/@id][@action='borrow'])]" />
请注意,匹配是genre/*
;这就是当前的背景。您的输入将是shelf1
。然后选择第二个xsl:apply templates
以book[@id=current()/@id]
开始。在我看来,如果它的id
属性等于shelf1
的id
属性,那么它看起来就像是在选择book
。为了测试这一点,您还可以将shelf1
的id
更改为1a
,这可能会起作用。(这就是第一个示例起作用的原因;shelf1的id
和book
匹配
我现在无法验证这个理论,但我认为这就是为什么比较似乎不起作用的原因。(它起作用了,你只是没有比较你认为你在比较的东西。)它应该有效。你有什么证据证明它无效?@MichaelKay请看一看添加的示例,我是否遗漏了什么?非常感谢much@John,缺少的是(a)对结果的描述,以及(b)对您预期结果的描述。“不起作用”太模糊了。(此外,如果您的示例数据是可用的,这也会有所帮助;事实上,它不是一个格式良好的XML文档。)@约翰:请提供一个小而完整的例子。人们在猜测——再一次!@DimitreNovatchev请看我添加的完整例子。谢谢。请看一看添加的例子,我遗漏了什么吗?上下文问题是什么意思?非常感谢。@John-就像Dimitre在评论中说的,请添加一个完整的例子。这包括一个XML示例,该示例在应用示例XSLT时再现了该问题。请查看我添加的示例,并告知我遗漏的位置。非常感谢您的回答正确!!它确实有效,意味着正在将shelf1 id与书籍id进行比较。但是如果我删除[@id=current()/@id]
在第二个xsl:apply-templates中,它仍然不起作用。有没有建议修复它,使其只比较图书id?非常感谢。
<r>
<elem id="AAA">one</elem>
<elem id="AAA">two</elem>
<elem id="ZZZ">three</elem>
</r>
AAA: has dup
AAA: has dup
<tests>
<test>
<elem id="1"/>
<elem id="1"/>
</test>
<test>
<elem id="AAA"/>
<elem id="AAA"/>
</test>
<test>
<elem id="BBB"/>
<elem id="CCC"/>
</test>
<test>
<elem id="2"/>
<elem id="3"/>
</test>
</tests>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="elem[1]">
<xsl:text>Comparing id </xsl:text>
<xsl:value-of select="concat('"',@id,'"
')"/>
<xsl:value-of select="concat('	',
boolean(following-sibling::*[@id=current()/@id]),
'
')"/>
</xsl:template>
</xsl:stylesheet>
Comparing id "1"
true
Comparing id "AAA"
true
Comparing id "BBB"
false
Comparing id "2"
false
<xsl:template match="genre/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="
book[@id=current()/@id][@action='extend']
[not( preceding-sibling::book[@id=current()/@id][@action='borrow'])]" />