如何在XSLT选择语句中使用“and”语句?

5
一个使用 AND 运算符的复合选择语句的例子是什么,类似于一个 if 语句,其中条件1 = true且条件2 = true?

你能否给出你目前的XSLT代码的示例以及一个输入的XML片段的例子? - rene
1
@Stephen:你需要一个使用and运算符的例子吗? - user357812
好问题,加一。请看我的答案,其中有一个完整且非常简单的示例。 :) - Dimitre Novatchev
@Alejandro - 我实际上找到了你一个带有“and”操作符的示例。谢谢。 - Stephen
3个回答

4

这是一个最简单的例子之一:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match=
    "num[. mod 2  = 0 and . mod 3 = 0]">
  <xsl:copy-of select="."/>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

当对这个XML文档应用此转换时:
<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

期望的、正确的结果已经生成:

<num>06</num>

3
这是一个使用复合选择语句的示例...
<?xml version="1.0" encoding="ISO-8859-1"?>
<A>
    <B>
        <C>1</C>
        <D>2</D>
    </B>
    <B>
        <C>1</C>
        <D>3</D>
     </B>
    <E>test</E>
</A>

如果您的当前模板匹配是 "E",那么请尝试以下代码,仅选择 C = 1 和 D = 3 的 B:对于 C 和 D,请阅读条件 1 = true 和条件 2 = true。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" indent="yes"/>
    <xsl:template match="E">
        <xsl:value-of select="../B[C = 1][D = 3]"></xsl:value-of>
    </xsl:template>
    <xsl:template match="C"/>
    <xsl:template match="D"/>
</xsl:stylesheet>

祝你好运


你的答案可能会导致误解... *[.='text' and position()=2] 并不等同于 *[.='text'][position()=2] - user357812
@Alejandro - 我更新了我的答案以反映这个问题,抱歉。我并没有打算误导任何人。 - BielsasBucket
1
实际上,A[B][C]等同于A[B and C],前提是(i) B和C都是布尔表达式而非数值表达式,且(ii) C不依赖于position()或last()。 - Michael Kay

1

使用AND运算符:

<xsl:if test="a and b">

我认为这个问题是关于SELECT语句的?不是IF语句,但可能我误解了。 “使用and运算符的复合SELECT语句的例子是什么?” - BielsasBucket
1
你说得对。一旦我看到了示例代码,我意识到自己错了。我会把这个留在这里,供未来的其他人参考。 - ed209

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接