title: Is it safe to concatenate two XPath 1.0 queries?
body: <p>If I have two XPath queries where the second one is meant to further drill down the result of the first, can I safely let my script combine them into a single query by...</p>
  
  <ol>
  <li>placing parenthesis around the first query,</li>
  <li>prefixing the second query with with a slash, and then</li>
  <li>simply concatenating the two strings ?</li>
  </ol>
  
  <h2>Context</h2>
  
  <p>The concrete usecase that sparked this question involves extracting information from XML/XHTML documents according to externally supplied pairs of <em>"CSS selector + attribute name"</em> using XPath behind the scenes.</p>
  
  <p>For example the script may get the following as input:</p>
  
  <p><strong>selector:</strong> <code>a#home, a.chapter</code><br>
  <strong>attribute:</strong> <code>href</code></p>
  
  <p>It then compiles the selector to an XPath query using the <a href="https://metacpan.org/pod/HTML%3a%3aSelector%3a%3aXPath" rel="nofollow">HTML::Selector::XPath</a> Perl module, and the attribute by simply prefixing a <code>@</code> ... which in this case would yield:</p>
  
  <p><strong>XPath query 1:</strong> <code>//a[@id='home'] | //a[contains(concat(' ', @class, ' '), ' chapter ')]</code><br>
  <strong>XPath query 2:</strong> <code>@href</code></p>
  
  <p>And then it repeatedly passes those queries to <a href="http://xmlsoft.org/" rel="nofollow">libxml2</a>'s XPath engine to extract the requested information (in this example, a list of URLs) from the XML documents in question.</p>
  
  <p>It works, but I would prefer to combine the two queries into a single one, which would simplify the code for invoking them and reduce the performance overhead:</p>
  
  <p><strong>XPath query:</strong> <code>(//a[@id='home'] | //a[contains(concat(' ', @class, ' '), ' chapter ')])/@href</code></p>
  
  <p>(note the added parenthesis and slash)</p>
  
  <p>But is this safe to do programmatically, for arbitrary input queries?</p>