0

I have the following query (XPath 2.0):

//xref[contains(@href,'#') and @class='- topic/xref ' and @type!='step' and @type!='fig' and @type!='substep']

As you can see, I want to find topic/xref elements with a hash in their href attribute. I want to exclude ceratin types of elements. Problem is, the above query does not display elements with @outputclass='expandable'

I had to run a seperate one to identify them:

//xref[contains(@href,'#') and @outputclass='expandable']

Why does the first, longer query, do not display those elements? I also tried contains(@class='- topic/xref ) instead of @class=' - topic/xref ' and it didn't help.

  • Add relevant `XML` source and specify required output as your issue is not quite clear – Andersson Jun 16 '17 at 10:16
  • @Andersson It's a huge DITA map with dozens of topics, and not really one that I can share. Here's an example of what I want to find: 1. 2. – kukunamuniu Jun 16 '17 at 10:25
  • So you want elements with `class="- topic/xref "` and `outputclass="expandable" `, right? and how about *while excluding those with other @*? What to exclude? – Andersson Jun 16 '17 at 10:30
  • I also want `class= '- topic/xref '` that do not have `outputclass='expandable'`. In fact, I want all `class='- topic/xref'` that do not have any of the following in their @type: fig, substep, table, whether they have `outputclass='expandable'` or not, but have a # character in their @href. – kukunamuniu Jun 16 '17 at 10:38

2 Answers2

0

Try below XPath:

//xref[@class="- topic/xref " and contains(@href, "#") and not(@type=("fig", "substep", "table") or @outputclass="expandable")]

that will return xref elements with class="- topic/xref " and # in href attribute but doesn't have type attribute with values "fig", "substep", "table" or outputclass attribute with value "expandable"

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I actually need the expandable ones. I modified what you sent and eventually used this: `//xref[@class="- topic/xref " and contains(@href, "#") and not(@type=("fig", "substep", "table", "step"))]` to do what I needed to do. Unlike my original query, this one did not omit the ones with @outputclass = expandable from the results. – kukunamuniu Jun 16 '17 at 11:29
0

You haven't shown your XML source, but I suspect you were expecting to retrieve elements having no @type attribute. However, the condition @type!='XYZ' is true only for an element that has an @type attribute whose value is something other than 'XYZ'. If my guess is correct, you intended not(@type='XYZ').

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks for the explanation of `!=`! In my case, I don't want to omit the @type attribute completely; it's a huge set of files that's existed for years and had many contributors, and it keeps surprising things in elements. Coupled with @Andersson's answer, I ended up with a query that did what I needed today. – kukunamuniu Jun 16 '17 at 11:34