XPath subtree pattern matching

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ahogue at theory dot lcs dot mit dot edu

    #1

    XPath subtree pattern matching

    Hello -

    Is there any way to match complex subtree patterns with XPath? The
    functions I see all seem to match along a single path from root to leaf.
    I would like to match full subtrees.

    For example, given the XHTML:

    <html>
    <body>
    <p>
    <a>#text</a>
    <br/>
    #text
    <b>#text</b>
    #text
    <br/>
    <font>
    <a>#text</a>
    </font>
    </p>
    <p>
    <a>#text</a>
    <br/>
    #text
    <br/>
    <font>
    <a>#text</a>
    </font>
    </p>
    </body>
    </html>

    I would like to construct a "pattern" using XPath to match all subtrees
    like:

    <p>
    <a>*</a>
    <br/>
    *
    (<b>*</b>)?
    (*)?
    <br/>
    <font>
    <a>*</a>
    </font>
    </p>

    where the "*" means that any text can be matched, and the "?" means that
    0 or 1 instances of the item may be matched, similar to a regular
    expression.

    Is there an easy way to do this kind of "subtree pattern matching" in
    XPath? Would I be better off writing a wrapper over XPath and using
    several XPath queries to represent and retreive my pattern?

    Thanks in advance,

    Andrew Hogue

  • Sebastian Schaffert

    #2
    Re: XPath subtree pattern matching

    "ahogue at theory dot lcs dot mit dot edu" <"ahogue at theory dot lcs dot
    mit dot edu"> wrote:
    [color=blue]
    > Hello -
    >
    > Is there any way to match complex subtree patterns with XPath? The
    > functions I see all seem to match along a single path from root to leaf.
    > I would like to match full subtrees.
    >[/color]

    XPath is basically a tree language, not a path language, so you *can*
    specify tree patterns. This is usually done by using qualifiers. To match
    e.g.

    <f>
    <a/>
    <b>Text</b>
    <c>Other Text</c>
    </f>

    and select "Text", an XPath expression could be used as follows:
    f/b[preceding-sibling::a][following-sibling::c]

    However, Tree matching in XPath has two restrictions:
    1. It is not "nice", since you basically encode the tree in a linear
    representation which is not straightforward , as it does not
    resemble the XML document
    2. It is not possible to select content at several positions (e.g.
    "Text" and "Other Text" together)

    I don't want to make too much advertisement again, but you might want to
    have a look at http://www.xcerpt.org if you want to have a look at a
    language with "real" tree patterns.

    --
    Sebastian

    PGP Key fingerprint =
    13 1D 2E 4F 20 3E C9 1F 4C 57 52 87 8A 80 48 4D F5 E9 97 EC

    Comment

    • Dimitre Novatchev

      #3
      Re: XPath subtree pattern matching

      As easy as:

      node()[count(ancestor-or-self::someNode | theRoot-someNode)
      =
      count(ancestor-or-self::someNode )
      ]

      This matches all nodes of the tree with root theRoot-someNode, which is a
      specific "someNode" element.

      In case we want simply to select all nodes of a given tree, we can use the
      following simpler XPath expression, which is not a match pattern, because
      the location steps (not the predicates) of a match pattern may only contain
      the child and attribute axis:

      theRoot-someNode//descendant-or-self::node()

      This selects all nodes of the tree with root a "theRoot-someNode" element.

      =====
      Cheers,

      Dimitre Novatchev.
      http://fxsl.sourceforge.net/ -- the home of FXSL



      "ahogue at theory dot lcs dot mit dot edu" <"ahogue at theory dot lcs dot
      mit dot edu"> wrote in message
      news:3f256f6f$0 $3949$b45e6eb0@ senator-bedfellow.mit.e du...[color=blue]
      > Hello -
      >
      > Is there any way to match complex subtree patterns with XPath? The
      > functions I see all seem to match along a single path from root to leaf.
      > I would like to match full subtrees.
      >
      > For example, given the XHTML:
      >
      > <html>
      > <body>
      > <p>
      > <a>#text</a>
      > <br/>
      > #text
      > <b>#text</b>
      > #text
      > <br/>
      > <font>
      > <a>#text</a>
      > </font>
      > </p>
      > <p>
      > <a>#text</a>
      > <br/>
      > #text
      > <br/>
      > <font>
      > <a>#text</a>
      > </font>
      > </p>
      > </body>
      > </html>
      >
      > I would like to construct a "pattern" using XPath to match all subtrees
      > like:
      >
      > <p>
      > <a>*</a>
      > <br/>
      > *
      > (<b>*</b>)?
      > (*)?
      > <br/>
      > <font>
      > <a>*</a>
      > </font>
      > </p>
      >
      > where the "*" means that any text can be matched, and the "?" means that
      > 0 or 1 instances of the item may be matched, similar to a regular
      > expression.
      >
      > Is there an easy way to do this kind of "subtree pattern matching" in
      > XPath? Would I be better off writing a wrapper over XPath and using
      > several XPath queries to represent and retreive my pattern?
      >
      > Thanks in advance,
      >
      > Andrew Hogue
      >[/color]


      Comment

      Working...