skip to Main Content

Is there an XML library where we divide the code into various components?

and on final we build/merge it

main.xml content is:

<?xml version="1.0" encoding="UTF-8"?>
<Component1>
  <SubComponent />
</Component1>

<Component2 />

Component1.xml, SubComponent.xml, Component2.xml content is:

<1-- xml code for component here -->

2

Answers


  1. SGML/XML has entities (think text macros) for reusing content at multiple places, and their use is explained in eg. Duplicate some parts of XML without rewriting them and XML configuration inheritance, avoid duplications.

    But the entity mechanism is considered part of the physical composition of documents rather than logical structure and as such doesn’t necessarily has to honor element boundaries. For example, a fragment included as an entity doesn’t have to contain just a single element, or could contain just text data and no tags at all, or could contain further entity references. This is more pronounced in SGML than it is in XML which puts certain restrictions on what can be contained in entity replacement text.

    A JavaScript library to process (expand) entities for Node.js is sgmljs available at https://www.npmjs.com/package/sgml. sgmljs also supports SGML templating which is closer to what you’re requiring in your description in that it expands a single element in a main document by an element having the same name (but with potentially different child content and attributes, and with parameters from the main document) read from a template document.

    sgmljs also runs in the browser to do all these things, but browser support out-of-the-box is configured as a drop-in for SGML server side processing to offload composition of documents onto the client. Arbitrary scripting for other applications in the browser, while certainly possible and even useful for in-browser checking and development tools, isn’t currently exposed via its API to a browser client.

    Login or Signup to reply.
  2. The kind of design you are looking for is pretty well the standard approach to XML processing with XSLT. You just define template rules for each component:

    <xsl:template match="top">
      <result>
        <xsl:apply-templates select="*"/>
      </result>
    </xsl:template>
    
    <xsl:template match="component-1">
      <!-- process component-1 -->
    </xsl:template>
    
    <xsl:template match="component-2">
      <!-- process component-2 -->
    </xsl:template>
    

    If you want greater modularity you can split the processing of different components into stylesheet modules, or use modes so the rules don’t interfere with each other. Current versions of XSLT allow you to break a document up into sections using grouping criteria, and to do things like sort+merge processing.

    Or you can go beyond that and implement a pipeline of separate transformations, perhaps orchestrated using XProc. The first step in the pipeline would split the input documents into parts, you would then process each part in a separate series of transformations, and the final step would recombine everything.

    You tagged the question javascript but didn’t say anything else about technology constraints. XSLT 3.0 processing is available in Javascript (both browser and node.js) using my company’s Saxon-JS product. I’m not aware of the status of XProc processors for the Javascript world, however: there was an implementation called Calumet, but I don’t know any details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search