This is the mail archive of the docbook@lists.oasis-open.org mailing list for the DocBook project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

SUMMARY: DocBook-XML and conditional sections


On Tuesday 28 September 1999, at 8 h 49, the keyboard of Stephane Bortzmeyer 
<bortzmeyer@debian.org> wrote:

> My documentations in DocBook-XML begin to grow and I would like to 
> conditionalize them, for instance for different software versions. in SGML, I

Thanks to Norm Walsh, I now have a solution which "works for me". Here is how 
I do it:

1) Creating a new common attribute.
Since I'm reluctant to use existing attributes, when their meaning is not 
clearly what I want, I present here the general case of adding a new common 
attribute. If you rely on an existing one, like "OS" or "Arch", skip this 
section.

I customize the DTD that way (Customizer's Guide, "Customizing Attribute Lists 
and Attributes"):

<?xml version="1.0" encoding="us-ascii"?>
<!DOCTYPE article PUBLIC "-//Norman Walsh//DTD DocBk XML V3.1//EN"
     "dtd/docbook-xml/docbookx.dtd"[
 <!ENTITY % local.common.attrib "frobnicate CDATA #IMPLIED">
]>

Therefore, I can write a document like the one in annex. And I can validate it 
and use any SGML tool on it.

2) Filtering the document.
The trick is to produce a new XML document, from the "master" document, for each value of the "frobnicate" attribute (having non mutually exclusive values of the attribute is let as an exercice to the reader). I do it with a simple Perl script (in annex), using the DOM API (I'm more used to SAX but DOM, although awfully slow, seems better for that purpose).

perl condition.pl frobnicate 1 with-conditions.db > frobnicate-1.xml
perl condition.pl frobnicate 2 with-conditions.db > frobnicate-2.xml
perl condition.pl frobnicate 3 with-conditions.db > frobnicate-3.xml

The resulting XML documents are also legal documents and can be processed by jade, as before.
<?xml version="1.0" encoding="us-ascii"?>
<!DOCTYPE article PUBLIC "-//Norman Walsh//DTD DocBk XML V3.1//EN"
     "dtd/docbook-xml/docbookx.dtd"[
 <!ENTITY % local.common.attrib "frobnicate CDATA #IMPLIED">
]>

<article>
  <artheader>
    <abstract>
      <para>Just a test</para>
    </abstract>
  </artheader>
  <sect1 frobnicate="3">
    <title>Section of frobnication 3</title>
    <para>Should be seen only for frobnication 3.</para>
  </sect1>
  <sect1>
    <title>Normal section, with a bit of frobnication 1</title>
    <para>Normal paragraph.</para>
    <para frobnicate="1">Should be seen only for frobnication 1.</para>
  </sect1>
  <sect1>
    <title>Normal section, with frobnication inside a
    paragraph</title>
    <para>Normal paragraph<phrase frobnicate="2"> but this sentence is
    only for frobnication 2</phrase>.</para>
  </sect1>
</article>
#!/usr/bin/perl

use XML::DOM;
use strict;

$::attributename = shift (@ARGV);
my $valuetokeep = shift (@ARGV);
@::valuestokeep = split (',', $valuetokeep);
my $filename = shift (@ARGV);

$::debug = 1;

if ((! $filename) or (! $valuetokeep) or (! $::attributename)) {
    die "Usage: $0 attributename valuetokeep filename\n" .
	" (Values to keep must be a comma-separated list)\n";
}

my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ($filename);
my $root = $doc->getDocumentElement;
my $attributes;

&scanChildren ($root->getChildNodes);
$doc->printToFileHandle (\*STDOUT);

sub scanChildren {
    my (@children) = @_;
    my ($value, $actualvalue, $found, $child);
  child:
    foreach $child (@children) {
	if ($child->getNodeType == ELEMENT_NODE) {
	    $actualvalue = $child->getAttribute ($::attributename);
	    if ($actualvalue) {
	      value:
		foreach $value (@::valuestokeep) {
		    if ($value eq $actualvalue) {
			$found = 1;
			last value;
		    }
		}
		if (! $found) {
		    $child->getParentNode->removeChild ($child);
		}
	    }
	    &scanChildren ($child->getChildNodes);
	}
    }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]