0

I'm unmarshaling a couple of large XML files. they have the common part and I decided to write the common parts in separate XML file and then include it using xi:include tag. it looks like this:

<tag1>
  <tag2>
  </tag2>
  <tag3>
  </tag3>
  <xi:include href = "long/common/part/of/partial/xml/file1"/>
  <xi:include href = "long/common/part/of/partial/xml/file2"/>
</tag1>

at this moment I would like to parametrize the long/common/part. I tried to define a variable using xsl:variable like this

 <xsl:variable name="test">
    "long/common/part/of/partial/xml/"
    </xsl:variable>

but the assigning value to href was a problem, neither the

<xi:include href = "{$test}"/>

or

<xi:include href = <xsl:value-of select="test"/>

wasn't working. Is there a way to assign value to XML attribute?

zapredelom
  • 1,009
  • 1
  • 11
  • 28

1 Answers1

2

You're mixing XInclude, XSLT, and ad-hoc {$var} syntax (not part of XML) here. What you can do to parametrize a href value in XInclude elements is to use an entity reference (XML's and SGML's mechanism for text substitution variables among other things):

<xi:include href="&href-value;"/>

where href-value must be bound to the string long/common/part/of/partial/xml/file1 either programmatically, or (preferably) by declaring it in the prolog eg:

<!DOCTYPE tag1 [
  <!ENTITY href-value "long/common/part/of/partial/xml/file1">
]>
<tag1>
 <xi:include href = "&href-value;"/>
</tag1>

However, since now you're using entity references anyway, you can achieve just the same with just entities, and without XInclude at all:

<!DOCTYPE tag1 [
  <!ENTITY common-part SYSTEM "long/common/part/of/partial/xml/file1">
]>
<tag1>
 &common-part;
</tag1>

This pulls the content of long/common/part/of/partial/xml/file1 into the common-part entity, then references that value in content, with the XML parser treating the document as if the replacement value for common-part (eg. whatever is stored in long/common/part/of/partial/xml/file1) had been specified directly in the document.

Hope this isn't too confusing; there's a general explanation how entities in XML and SGML work in this answer

imhotap
  • 2,275
  • 1
  • 8
  • 16
  • this is a perfect answer to my question, but if you also can hint me how can I bound href-value programmatically( just a link will be fine) you'll be my hero for today @imhotap – zapredelom Jun 10 '19 at 11:07
  • @zapredelom That depends a bit on which Java API for XML you're using. The most common way to do this is to use the SAX Resolver API as explained in [this article from 2003](https://www.techrepublic.com/article/resolve-custom-xml-entities-with-sax-and-java/). Note the SAX Resolver API requires you to declare an entity with a system or public identifier (like in the way I wrote in the answer with a system identifer eg. file name) and than lets you customize the replacement text for that system or public identifier, but not the replacement text for an entity name as such – imhotap Jun 10 '19 at 11:19