title: Behavior of relative URIs in XML when declaration source is parameter entity replacement text
body: <p>I’m trying to understand the correct behavior of relative system ID URIs. First, let me quote the spec:</p>
  
  <blockquote>
    <p>4.2.2 External Entities</p>
    
    <p>[...]</p>
    
    <p>[The location of the resource within which the entity declaration occurs] is defined to be the external entity containing the '&lt;' which starts the declaration, at the point when it is parsed as a declaration.</p>
    
    <p>A URI might thus be relative to the document entity, to the entity containing the external DTD subset, or to some other external parameter entity. [...]</p>
  </blockquote>
  
  <p>On first reading, I thought these two statements were contradictory. It seemed to me that the parsing of any text <em>“as a declaration”</em> may only occur in two contexts: an internal subset or an external subset. This seems to follow naturally from the fact that entity dereferencing/expansion is recursive. So how can "some other external parameter entity" ever be what an ID is relative to?</p>
  
  <p>For both statements to be true, perhaps the phrase "point when it is parsed" just doesn’t mean what I took it to mean. Could the "point" here refer instead to whatever context defined the source text?</p>
  
  <p>I’m going to provide an example that might help make this question more imminently answerable.</p>
  
  <p>First, our document. This could be anywhere, since the external DTD it references uses an absolute URL for a system ID.</p>
  
  <pre><code>FILE: doc.xml
  
      &lt;!DOCTYPE foo SYSTEM "http://dotcom.xml/foo.dtd"&gt;
      ...
  </code></pre>
  
  <p>So next up is the DTD — unambiguously to be found at <code>http://dotcom.xml/foo.dtd</code>:</p>
  
  <pre><code>FILE: http://dotcom.xml/foo.dtd
  
      &lt;!ENTITY % bar SYSTEM "bar/bar.ent"&gt;
      %bar;
  </code></pre>
  
  <p>Still nothing ambiguous. It’s clear that our next resource is to be found at <code>http://dotcom.xml/bar/bar.ent</code></p>
  
  <pre><code>FILE: http://dotcom.xml/bar/bar.ent
  
      &lt;!ENTITY % baz SYSTEM "baz/baz.ent"&gt;
      %baz;
  </code></pre>
  
  <p>But here’s where I get unsure. Given that the spec specifically states that it is possible for a path to be relative to an external parameter entity, the only thing I can think of is that here, the absolute path for the <code>baz</code> entity must be <code>http://dotcom.xml/bar/baz/baz.ent</code>.</p>
  
  <p>This seemed odd to me because the content of an external parameter entity is, beyond the text declaration, just a blob of text whose context and meaning is unknowable until later, when (and if) it is referenced in either an internal or external subset. But it’s not crazy — keeping track of the origin in simple enough.</p>
  
  <p>But why, then, does the spec qualify their statement with "at the point when it is parsed"? Well, it could be that <em>this</em> is different:</p>
  
  <pre><code>FILE: http://dotcom.xml/foo.dtd
  
      &lt;!ENTITY % bar SYSTEM "bar/bar.ent"&gt;
      %bar;
      %baz;
  
  FILE: http://dotcom.xml/bar/bar.ent
  
      &lt;!ENTITY % baz SYSTEM "baz/baz.ent"&gt;
  </code></pre>
  
  <p>But that doesn’t seem to work. I’m pretty sure it doesn’t make sense to say that the relative context is different now, because the entity declaration’s <code>&lt;</code> still "happened" in <code>bar.ent</code>. The spec specifically calls that point out. In fact, if the location of the reference mattered, then it would seem to have to always be <code>http://dotcom.xml/baz/baz.ent</code>, since the actual expansion takes place "back at home" in <code>foo.dtd</code> no matter how many intermediate parameter entities there are between it and <code>baz</code>.</p>
  
  <p>So I’m looking to understand two things:</p>
  
  <ol>
  <li><p>Above, what is the correct absolute URL for <code>"baz/baz.ent"</code>?</p></li>
  <li><p>a. If it is <code>http://dotcom.xml/bar/baz/baz.ent</code>, why does the spec says "at the point where it is parsed"?</p>
  
  <p>b. If it is <code>http://dotcom.xml/baz/baz.ent</code>, why does the spec say "or to some other external parameter entity"?</p></li>
  </ol>