English 中文(简体)
查找除小节外的所有后端文本节点
原标题:Find all descendant text() nodes except in subsections

我的 XML 文档任意嵌套了部分。 鉴于引用了特定章节, 我需要在该节中找到所有 < code> TextNode 。 不包括小节

例如,如果引用下面的 节点,我只需要找到“A1”和“A1”文本节点:

<root>
  <section id="a1">
    <b>A1 <c>A1</c></b>
    <b>A1 <c>A1</c></b>
    <section id="a1.1">
      <b>A1.1 <c>A1.1</c></b>
    </section>
    <section id="a1.2">
      <b>A1.2 <c>A1.2</c></b>
      <section id="a1.2.1">
        <b>A1.2.1</b>
      </section>
      <b>A1.2 <c>A1.2</c></b>
    </section>
  </section>
  <section id="a2">
    <b>A2 <c>A2</c></b>
  </section>
</root>

若非明显, 则上述数据为编造数据。 特别是 id 属性在真实世界文档中可能不存在 。

现在我想到的最好办法就是 找到这个区域里的所有文本节点 然后用Ruby来减掉那些我不想的节点

def own_text(node)
  node.xpath( .//text() ) - node.xpath( .//section//text() )
end

doc = Nokogiri.XML(mydoc,&:noblanks)
p own_text(doc.at("#a1")).length #=> 4

我可以制作一个 XPath 1. 0 的单一表达式来直接找到这些节点吗? 类似 :

.//text()[ancestor::section = self] # self being the original context node
最佳回答

use (对于有 id 属性的带字符串值为“ a1” 的区段):

   //section[@id= a1 ]
       //*[normalize-space(text()) and ancestor::section[1]/@id =  a1 ]/text()

<强 > 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=
      "//section[@id= a1 ]
           //*[normalize-space(text()) and ancestor::section[1]/@id =  a1 ]
     "/>
 </xsl:template>
</xsl:stylesheet>

在提供的 XML 文档中应用此转换时 < 强> :

<root>
    <section id="a1">
        <b>A1 
            <c>A1</c>
        </b>
        <b>A1 
            <c>A1</c>
        </b>
        <section id="a1.1">
            <b>A1.1 
                <c>A1.1</c>
            </b>
        </section>
        <section id="a1.2">
            <b>A1.2 
                <c>A1.2</c>
            </b>
            <section id="a1.2.1">
                <b>A1.2.1</b>
            </section>
            <b>A1.2 
                <c>A1.2</c>
            </b>
        </section>
    </section>
    <section id="a2">
        <b>A2 
            <c>A2</c>
        </b>
    </section>
</root>

<强度> 它评估 XPath 表达式( 只选择想要的文本节点的父母 -- -- 以便有清晰可见的结果), 并将选中的节点复制到输出 :

<b>A1 
            <c>A1</c>
</b>
<c>A1</c>
<b>A1 
            <c>A1</c>
</b>
<c>A1</c>

UPDATE :如果 section 元素具有相同的 id 属性(或根本没有 id 属性),则使用:

       (//section)[1]
           //*[normalize-space(text())
           and
              count(ancestor::section)
             =
               count((//section)[1]/ancestor::section) +1]/text()

<强 > 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=
          "(//section)[1]
               //*[normalize-space(text())
               and
                  count(ancestor::section)
                 =
                   count((//section)[1]/ancestor::section) +1]
         "/>
     </xsl:template>
</xsl:stylesheet>

<强度 > 转换结果(相同) :

<b>A1 
            <c>A1</c>
</b>
<c>A1</c>
<b>A1 
            <c>A1</c>
</b>
<c>A1</c>

此选项选择完全相同的通缉文本节点 。

问题回答

使用 :

//text()[ancestor::section[1]/@id =  a1 ]




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...