How should i retrieve nodes where ALL children have gender = "male".
使用:
/*/parent[child
and
(not(child[not(@gender) or not(@gender='male')]))
]
これにより、次のすべてのparent
要素が選択されます。
XML ドキュメントの最上位要素の子であり、かつ
子供がいchild
て、
すべてのchild
子がgender
属性を持ち、
すべてchild
の子供のgender
属性が「男性」の文字列値を持つもの
ここでは、非常に一般的で便利な二重否定の法則を使用しました。
2 番目の質問:
Q2 また、子供が 1 人だけ (男性) のすべての親を取得するにはどうすればよいですか。
使用:
/*/parent[child[@gender='male']
and
not(child[2])
]
これにより、以下が選択されます。
parent
XML ドキュメントの最上位要素の子である任意の要素、および
属性の文字列値が「male」であるchild
子があり、かつgender
child
それは二人目の子供がいません。
XSLT ベースの検証:
<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>
この変換が次の XML ドキュメント(より代表的なものにするために拡張された、提供されたもの) に適用される場合:
<t>
<parent> a
<child gender="male">b</child>
<child gender="female">c</child>
<child gender="female">d</child>
</parent>
<parent> e
<child gender="male">f</child>
<child gender="male">g</child>
<child gender="female">h</child>
</parent>
<parent> z
<child gender="male">x</child>
<child gender="male">y</child>
</parent>
<parent> t
<child gender="male">u</child>
</parent>
</t>
2 つの XPath 式が評価され、これらの評価の結果 (選択された要素) が output にコピーされます。
<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>