Difference between revisions of "Command/xmlinclude"

From Wiki
Jump to navigation Jump to search
(Create entire page)
 
m (Inserted Hans' explanation from mailing list.)
 
(2 intermediate revisions by one other user not shown)
Line 33: Line 33:
 
* If no attribute is specified, {{code|href}} is assumed.  
 
* If no attribute is specified, {{code|href}} is assumed.  
 
* If the file is empty or not found, the inclusion tag is replaced with nothing.  
 
* If the file is empty or not found, the inclusion tag is replaced with nothing.  
* Inclusion is recursive: if the tree you merge in itself contains inclusion tags, they are acted on, too.
+
* Inclusion is recursive: if there are inclusion tags in the external tree you pull in, then they, in turn are acted on, too. If you do not want recursive inclusion, you must use the {{code|xml.include))}} command directly, like so:
 +
 
 +
\ctxlxml{include("main","include","filename|href",false)} % false means no recursion
  
 
== Example ==
 
== Example ==
 +
 +
Here is an example how to use {{cmd|xmlinclude}} to combine several xml files into one document. The xml files represent single chapters of a book project. Their structure is:
  
 
<xmlcode>
 
<xmlcode>
<example>
+
<chapter title="Chapter 1">
   <chapter>text 1</chapter>
+
   <content>
   <chapter>text 2</chapter>
+
    Text of chapter
</example>
+
   </content>
 +
</chapter>
 
</xmlcode>
 
</xmlcode>
 +
 +
Then, one master file combines all these into one structure via the xi:include element [http://www.w3.org/TR/2003/WD-xinclude-20031110/]:
 +
 +
<xmlcode>
 +
<document>
 +
  <xi:include href="chapter_1.xml"/>
 +
  <xi:include href="chapter_2.xml"/>
 +
</document>
 +
</xmlcode>
 +
 +
And finally, you process this with a master tex file:
  
 
<texcode>
 
<texcode>
\xmlinclude...
+
\startxmlsetups xml:setups
 +
    \xmlinclude{#1}{xi:include}{href}
 +
    \xmlsetsetup{#1}{document|chapter}{xml:*}
 +
\stopxmlsetups
 +
 
 +
\xmlregistersetup{xml:setups}
 +
 
 +
\startxmlsetups xml:document
 +
    \xmlflush{#1}
 +
\stopxmlsetups
 +
 
 +
\startxmlsetups xml:chapter
 +
    \startchapter[title=\xmlatt{#1}{title}]
 +
        \xmlall{#1}{content}
 +
    \stopchapter
 +
\stopxmlsetups
 +
 
 +
\starttext
 +
 
 +
    \xmlprocessfile{main}{master.xml}{}
 +
 
 +
\stoptext
 
</texcode>
 
</texcode>
-->
 
  
 
== See also ==
 
== See also ==
Line 58: Line 94:
  
 
[[Category:Command/XML|xmlinclude]]
 
[[Category:Command/XML|xmlinclude]]
!--
 
-->
 

Latest revision as of 17:16, 17 December 2012

\xmlinclude

Syntax

\xmlinclude{...}{...}{...}
{...} xmldata, usually main
{...} pattern that matches tags that stand for 'include external file'. Default: include
{...} names of attribute containing URL of file to include, separated by pipes. Default: href

Description

\xmlinclude{main}{include}{filename|href|name} merges XML trees in external files into the tree called main. External files are merged in at every include tag; which file to merge in is determined by looking at the filename attribute; if that is absent the href and name attributes are tried in order.

Further behaviour:

  • If no attribute is specified, href is assumed.
  • If the file is empty or not found, the inclusion tag is replaced with nothing.
  • Inclusion is recursive: if there are inclusion tags in the external tree you pull in, then they, in turn are acted on, too. If you do not want recursive inclusion, you must use the xml.include)) command directly, like so:
\ctxlxml{include("main","include","filename|href",false)} % false means no recursion

Example

Here is an example how to use \xmlinclude to combine several xml files into one document. The xml files represent single chapters of a book project. Their structure is:

<chapter title="Chapter 1">
  <content>
    Text of chapter
  </content>
</chapter>

Then, one master file combines all these into one structure via the xi:include element [1]:

<document>
  <xi:include href="chapter_1.xml"/>
  <xi:include href="chapter_2.xml"/>
</document> 

And finally, you process this with a master tex file:

\startxmlsetups xml:setups
    \xmlinclude{#1}{xi:include}{href}
    \xmlsetsetup{#1}{document|chapter}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:setups}

\startxmlsetups xml:document
    \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:chapter
    \startchapter[title=\xmlatt{#1}{title}]
        \xmlall{#1}{content}
    \stopchapter
\stopxmlsetups

\starttext

    \xmlprocessfile{main}{master.xml}{}

\stoptext 

See also

Help from ConTeXt-Mailinglist/Forum

All issues with: