简体   繁体   English

如何根据XSLT 2.0中的子元素检索特定的XML元素?

[英]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.

相关问题 如何计算第一个的指数位置 <w:p> 基于XSLT 2.0中的子属性值的元素? - How do I calculate index position of first <w:p> element based on its child attribute value in XSLT 2.0? 使用xslt我如何解决xml文件中的此特定元素? - using xslt how do i address this particular element in an xml file? 如何通过XSLT转换选择XML中的特定节点或元素? - How do I select a particular node or element in an XML with XSLT transformations? 使用XSLT 2.0根据元素值和属性对XML元素进行分组 - Group XML Elements based on element values and attributes using XSLT 2.0 如何在xslt 2.0中获得与此场景有关的特殊XML元素索引? - How to get the Particular XML elements indexes with respect to this scenario in xslt 2.0? 在XSLT 1.0中,当特定XML元素是多个具有相同名称的元素之一时,如何访问它的属性? - In XSLT 1.0, how can I access attributes of a particular XML element when it is one of multiple elements with the same name? 如何使用XSLT1.0在XML文件中替换ID元素及其所有参考元素 - How can I replace an ID element AND all of its reference elements in an XML file using XSLT1.0 如何使用xslt 2.0检索xml的名称空间 - how to retrieve namespace of an xml using xslt 2.0 如何使用xslt2.0在xml文件中以相同的顺序获取特定的xml元素值? - How to get the particular xml element values in the same order in xml file using xslt2.0? 如何根据其属性值删除 XML 元素 - How do I remove an XML element based on the value of its attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM