XPATH 1.0相当于SQL中的ALL
XPATH 1.0相当于在SQL中使用嵌套查询和“ALL”关键字。例如,考虑下面的片段。< /P>XPATH 1.0相当于SQL中的ALL,xpath,Xpath,XPATH 1.0相当于在SQL中使用嵌套查询和“ALL”关键字。例如,考虑下面的片段。< /P> <parent> a <child gender = "male">b</child> <child gender = "female>c</child></parent> <parent> z <child gender = "male"> y </child></parent>
<parent> a <child gender = "male">b</child>
<child gender = "female>c</child></parent>
<parent> z <child gender = "male"> y </child></parent>
ab
全部:选择有子女但没有非男性子女的父母:
//父[子与非子[@gender!=“male”]]
选择只有一个男婴的家长-选择只有一个男婴的家长:
//父项[count(child)=1和子项[@gender=“male”]]
使用:
/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]
/*/parent[child[@gender='male']
and
not(child[2])
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]"/>
==========
<xsl:copy-of select=
"/*/parent[child[@gender='male']
and
not(child[2])
]"/>
</xsl:template>
</xsl:stylesheet>
<parent> z
<child gender="male">x</child>
<child gender="male">y</child>
</parent>
<parent> t
<child gender="male">u</child>
</parent>
==========
<parent> t
<child gender="male">u</child>
</parent>
此选项选择所有父元素
元素:
是XML文档顶部元素的子元素,并且
有一个子对象
子对象,并且
其所有child
子项都具有gender
属性,并且
其所有child
children的gender
属性的字符串值为“male”
这里我们使用了非常通用和有用的
你的第二个问题:
/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]
/*/parent[child[@gender='male']
and
not(child[2])
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]"/>
==========
<xsl:copy-of select=
"/*/parent[child[@gender='male']
and
not(child[2])
]"/>
</xsl:template>
</xsl:stylesheet>
<parent> z
<child gender="male">x</child>
<child gender="male">y</child>
</parent>
<parent> t
<child gender="male">u</child>
</parent>
==========
<parent> t
<child gender="male">u</child>
</parent>
问题2:我如何检索只有一个(男性)的所有父母
孩子
使用:
/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]
/*/parent[child[@gender='male']
and
not(child[2])
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]"/>
==========
<xsl:copy-of select=
"/*/parent[child[@gender='male']
and
not(child[2])
]"/>
</xsl:template>
</xsl:stylesheet>
<parent> z
<child gender="male">x</child>
<child gender="male">y</child>
</parent>
<parent> t
<child gender="male">u</child>
</parent>
==========
<parent> t
<child gender="male">u</child>
</parent>
这将选择:
/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]
/*/parent[child[@gender='male']
and
not(child[2])
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]"/>
==========
<xsl:copy-of select=
"/*/parent[child[@gender='male']
and
not(child[2])
]"/>
</xsl:template>
</xsl:stylesheet>
<parent> z
<child gender="male">x</child>
<child gender="male">y</child>
</parent>
<parent> t
<child gender="male">u</child>
</parent>
==========
<parent> t
<child gender="male">u</child>
</parent>
作为XML文档顶部元素的子元素的任何父元素,以及
具有其性别属性的字符串值为“男性”的子对象的
没有第二个子项的
基于XSLT的验证:
/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]
/*/parent[child[@gender='male']
and
not(child[2])
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]"/>
==========
<xsl:copy-of select=
"/*/parent[child[@gender='male']
and
not(child[2])
]"/>
</xsl:template>
</xsl:stylesheet>
<parent> z
<child gender="male">x</child>
<child gender="male">y</child>
</parent>
<parent> t
<child gender="male">u</child>
</parent>
==========
<parent> t
<child gender="male">u</child>
</parent>
==========
当此转换应用于以下XML文档时(提供的文档扩展为更具代表性):
A.
B
C
D
E
F
G
H
Z
x
Y
T
U
对两个XPath表达式求值,并将这些求值的结果(选定元素)复制到输出中:
/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]
/*/parent[child[@gender='male']
and
not(child[2])
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]"/>
==========
<xsl:copy-of select=
"/*/parent[child[@gender='male']
and
not(child[2])
]"/>
</xsl:template>
</xsl:stylesheet>
<parent> z
<child gender="male">x</child>
<child gender="male">y</child>
</parent>
<parent> t
<child gender="male">u</child>
</parent>
==========
<parent> t
<child gender="male">u</child>
</parent>
z
x
Y
T
U
==========
T
U
如何检索所有子节点都具有gender=“male”的节点
一些受访者认为这意味着必须至少有一个孩子。但是您要求使用SQL的ALL运算符,我相信在SQL中(就像在数理逻辑中一样),如果应用于空集,ALL将返回true。(所有独角兽都有两只角-给我看一只没有的。)
因此,我认为对你的问题的严格回答将删除迪米特在回答中包含的“孩子和”。但迪米特里可能已经猜对了,你所要求的并不是你真正想要的