1

I have done a search for all nodes that have an attribute containing (substring) a String. These nodes can be found at different levels of the tree, sometimes 5 or 6 levels deep. I'd like to know what parent/ancestor node they correspond to at a specified level, 2 levels deep. The result for the search only should be much greater than the results for the corresponding parents.

EDIT to include code: /xs:schema/xs:element/descendant::node()/@*[starts-with(., 'my-search-string-here')]

EDIT to clarify my intent:

When I execute the Xpath above sometimes the results are

/xs:schema/xs:element/xs:complexType/xs:attribute or /xs:schema/xs:element/xs:complexType/xs:sequence/xs:element or /xs:schema/xs:element/xs:complexType/xs:complexContent/xs:extension/xs:sequence/xs:element

These results indicate a place in the Schema where I have added application specific code. However, I need to remove this code now. I'm building an "adapter" schema that will redefine the original Schema (untouched) and import my schema. The String I am searching for is my prefix. What I need is the @name of the /xs:schema/node() in which the prefix is found, so I can create a new schema defining these elements. They will be imported into the adapter and redefine another schema (that I'm not supposed to modify).

To reiterate, I need to search all the attributes (descendants of /xs:schema/xs:element) for a prefix, and then get the corresponding /xs:schema/xs:element/@name for each of the matches to the search.

emdog4
  • 1,975
  • 3
  • 20
  • 25

3 Answers3

1

To reiterate, I need to search all the attributes (descendants of /xs:schema/xs:element) for a prefix, and then get the corresponding /xs:schema/xs:element/@name for each of the matches to the search.

 /
 xs:schema/
  xs:element
  [descendant::*/@*[starts-with(., 'my-search-string-here')]]/
   @name
Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67
1

This should do it:

/xs:schema/xs:element[starts-with(descendant::node()/@*, 'my-search-string-here')]

You want to think of it as

select the xs:elements which contain a node with a matching attribute

rather than

select the matching attributes of descendant nodes of xs:elements, then work back up

Eric
  • 95,302
  • 53
  • 242
  • 374
  • I'm sorry for the confusion, but this is the code it needs to work `/xs:schema/node()[@*[starts-with(., 'my-search-string-here')]]` – emdog4 Sep 13 '11 at 21:20
  • You shouldn't need the second set of square brackets. In what way does mine not work? Also, yours does not look for descendants, but looks for children. – Eric Sep 13 '11 at 21:22
  • see this recent answer: http://stackoverflow.com/questions/7405212/xpath-to-search-for-a-node-that-has-any-attribute-containing-a-specific-string/7405749#7405749 – emdog4 Sep 13 '11 at 21:28
  • I believe this gives the result I'm looking for: `/xs:schema/child::node()[descendant::node()/@*[starts-with(., 'my-search-string-here')]]` – emdog4 Sep 13 '11 at 21:30
  • Yes, but in this case, my code works as well, since you don't care about the element set returned in the condition. Oh, I see what you mean now. – Eric Sep 13 '11 at 21:35
0

As Eric mentioned, I need to change my thought process to select the xs:elements which contain a node with a matching attribute rather than select the matching attributes of descendant nodes of xs:elements, then work back up. This is critical. However, the code sample he posted to select the attributes does not work, we need to use another solution.

Here is the code that works to select an element that contains and attribute containing* (substring) a string.

/xs:schema/child::node()[descendant::node()/@*[starts-with(., 'my-prefix-here')]]

emdog4
  • 1,975
  • 3
  • 20
  • 25