[英]How to do I retrieve a particular XML element based on its child elements in XSLT 2.0?
This is my XML Document (small snippet). 这是我的XML文档(小片段)。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p> <!-- Current Node -->
<w:r>
<w:t>
This is the
</w:t>
</w:r>
<w:r>
<w:pict>
<w:p>
<w:r>
<w:t>
I dont need this
</w:t>
</w:r>
</w:p>
</w:pict>
</w:r>
<w:r>
<w:pict>
<w:p>
<w:r>
<w:t>
I dont need this too
</w:t>
</w:r>
</w:p>
</w:pict>
</w:r>
<w:r>
<w:t>
text that i need to retrieve...
</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
Here, I want to retrieve <w:r><w:t>
value and that <w:r>
should not contain child <w:pict>
inside it. 在这里,我想检索<w:r><w:t>
值,并且<w:r>
不应该在其中包含子<w:pict>
。 So, as per my above XML Document I want to generate the following output: 所以,根据我上面的XML文档,我想生成以下输出:
<paragraph>This is the text that i need to retrieve...</paragraph>
and this is my XSLT snippet (please tell me what changes does this XSLT require to get above format): 这是我的XSLT片段(请告诉我这个XSLT需要哪些更改才能获得上述格式):
<xsl:choose>
<xsl:when test="self::w:p[//w:r/w:t[not(ancestor::w:pict)]]">
<Paragraph>
<xsl:apply-templates select="./w:t[not(ancestor::w:pict)]" />
</Paragraph>
</xsl:when>
</xsl:choose>
<xsl:template match="w:t">
<xsl:value-of select="." />
</xsl:template>
but it is not working... 但它不起作用......
Please guide me to get out of this issue. 请指导我解决这个问题。
In your current example, if your current node is /w:document/w:body/w:p
you can retrive all your desired nodes with: 在当前示例中,如果当前节点是/w:document/w:body/w:p
,则可以使用以下命令检索所有需要的节点:
w:r/w:t
However, if you need to retrive w:r/w:t
on any level but with out w:pict
as ancestor, you could use the ancestor
axis as such: 但是,如果您需要在任何级别上检索w:r/w:t
但使用w:pict
作为祖先,则可以使用ancestor
轴:
.//w:r/w:t[not(ancestor::w:pict)]
Assuming you are processing this in classic XSLT fashion using top-down recursive descent with xsl:apply-templates, then the way to exclude aw:r that has aw:pict child is like this: 假设您使用自上而下的递归下降和xsl:apply-templates以经典的XSLT方式处理它,那么排除具有aw:pict子的aw:r的方式是这样的:
<xsl:template match="w:r[w:pict]"/>
I seem to remember coming across a case where I wanted to exclude aw:r if its only child element was aw:pict. 我似乎记得遇到过我想要排除aw:r的情况,如果它唯一的子元素是aw:pict。 In that case the solution would be 在那种情况下,解决方案将是
<xsl:template match="w:r[w:pict and count(*)=1]"/>
You can also use filtering to retrieve all w:r
nodes which do not have w:pict
element 您还可以使用过滤来检索所有没有w:pict
元素的w:r
节点
<xs:for-each select="//w:r[count(w:pict) = 0]">
<xsl:value-of select="w:paragraph" />
</xsl:for-each>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.