Project root relative paths

Questions about XML that are not covered by the other forums should go here.
odinodin
Posts: 7

Project root relative paths

Tue Apr 21, 2015 10:56 am

We have a large project that is split into several files. Is it possible to include files through paths that are relative to the project root directory (i.e not relative to the current file)?

For example:

Code: Select all

root
  a
    file1.xml
  b
    file2.xml


Is there a way to reference file1 from file2 by saying "a/file1.xml" (relative to the project) instead of "../a/file1.xml" (relative to the file2.xml file) ?

Our current solution is to define an entity "projectRoot" which has the absolute path of the project directory. All other paths are then resolved based on the "projectRoot" entity. This is bad because it is not portable across users and operating systems.
adrian
Posts: 2459

Re: Project root relative paths

Tue Apr 21, 2015 11:56 am

Hi,

First I must ask, why do you want to avoid relative references?
They are cross platform and most XML refactoring tools can handle them without issues.
In the long run you would probably be better off with the relative references rather than the hacks needed to avoid them.


Whether this is possible or not, depends on the type of reference and the type of document (DocBook DITA, etc) you're using.

For example, if you're using DocBook with XInclude, it's possible to specify a different base (xml:base) than the current file and every reference will be resolved relative to that specified base. But you will still have to specify explicitly that base at least once on the root of the document by either an absolute URI (file:///...) or a reference relative to the location of the current file (so you would still have to do this at least once per document, or customize the DTD/schema to do this for all).
e.g. file2.xml

Code: Select all

<article xmlns="http://docbook.org/ns/docbook" version="5.0"
    xmlns:xlink="http://www.w3.org/1999/xlink" xml:base="..">
  <xi:include href="a/file1.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
...


There is another way to do something like this with the help of a custom URI and an XML catalog that rewrites that URI, but you will then depend on the XML catalog. The advantage is this is portable.
You need a <protocol>:<name> at the start of every URI. Or you could use a URN (urn:<NID>:<NSS>).
e.g.
Reference in file2: tst:myproject/a/file1.xml
Create the XML catalog file in the project root folder. It will resolve/rewrite the URIs and systemIDs relative to the project root:

Code: Select all

<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
    <rewriteURI uriStartString="tst:myproject/" rewritePrefix="./"/>
    <rewriteSystem systemIdStartString="tst:myproject/" rewritePrefix="./"/>
</catalog>

Add this XML catalog file in Oxygen's Options > Preferences, XML / XML Catalog.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
odinodin
Posts: 7

Re: Project root relative paths

Wed Apr 22, 2015 9:42 am

Thank you for your extensive answer, it's good to know that there is a proper solution.

I tried to make your suggestion work in our Docbook based project. In the catalog file I put the following:

Code: Select all

<catalog prefer="system" xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
    <rewriteURI rewritePrefix="../../" uriStartString="tst:myproject/"></rewriteURI>
    <rewriteSystem rewritePrefix="../../" systemIdStartString="tst:myproject/"></rewriteSystem>

....


And in one of the files that contain a chapter which needs to include a file:

Code: Select all

<xi:include href="tst:myproject/some_folder/some.xml" xmlns:xi="http://www.w3.org/2001/XInclude"></xi:include>


When generating PDF with FOP, Oxygen gives the following error message: SAXException Malformed URL tst:myproject/some_folder/some.xml

Any ideas of what might be wrong?
adrian
Posts: 2459

Re: Project root relative paths

Wed Apr 22, 2015 11:59 am

Hi,

What transformation scenario (default or custom) are you running and in what version of Oxygen (Help > About)?
It works for me in v16.1 with the default DocBook PDF transformation scenario.
Oxygen passes the XML catalog as a parameter (-Dxml.catalog.files) when running Apache FOP.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
odinodin
Posts: 7

Re: Project root relative paths

Thu Apr 23, 2015 11:03 am

I upgraded to 16.1 from 16 but the same error occured. The transformation scenario is customized, so I have to look in to that. I'll report back if I find anything.

Anyway, thank your for the help so far.

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests