javaxmljaxbxinclude

paremetrize part of xml attribute value


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?


Solution

  • 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