views:

44

answers:

2

I have used an XSL to convert a docbook document into dita. The conversion was successful, bu there are unwanted artifacts which remain. I've gotten rid of a few of them, but I have saved these changes in another XSL which is run after the conversion. My question is: Is it possible to combine this into a single XSL? when I've tried adding the code after the docbook2dita.xsl it seemed to have no effect, am I missing something?

Also I was thinking of changing the xref that were being used in docbook, to related links in DITA. Is this a good idea? what problems have ppl run into with xref that related links needed to be created?

A: 

Is it possible to combine this into a single XSL?

It's posible to express this in a single transformation with multiple stylesheets modules.

If you don't know how DITA XSLT works, the best way would be: to use import declaration for XSLT DITA, and to declare your own rules.

Edit: Example. Suppose this stylesheet base.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="root">
        <html>
            <xsl:apply-templates/>
        </html>
    </xsl:template>
    <xsl:template match="parent">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
    <xsl:template match="child">
        <b>
            <xsl:apply-templates/>
        </b>
    </xsl:template>
</xsl:stylesheet>

With this input:

<root>
    <parent>
        <child>1</child>
    </parent>
    <parent>
        <child>2</child>
    </parent>
    <parent>
        <child>3</child>
    </parent>
    <parent>
        <child>4</child>
    </parent>
</root>

Output:

<html>
    <p>
        <b>1</b>
    </p>
    <p>
        <b>2</b>
    </p>
    <p>
        <b>3</b>
    </p>
    <p>
        <b>4</b>
    </p>
</html>

Now, this stylesheet ussing import.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:import href="base.xsl"/>
    <xsl:template match="parent">
        <div>
            <xsl:apply-templates/>
        </div>
    </xsl:template>
</xsl:stylesheet>

Output:

<html>
    <div>
        <b>1</b>
    </div>
    <div>
        <b>2</b>
    </div>
    <div>
        <b>3</b>
    </div>
    <div>
        <b>4</b>
    </div>
</html>
Alejandro
Are transformations applied sequentially, or are they all concurrent?
Ace
@Ace: With this method you are declaring **one** trasformation with several modules. So, it's all concurrent because there is no sequence.
Alejandro
So if I had like 8 transformations (the first being docbook2dita.xsl, then the rest order doesn't matter), do I have one 'master' .XSL that imports each of them? or have each import one other one, so it's like a chain of XSL that gets called when the first one gets called?
Ace
@Ace: Order or chain does matter. Suppose this chain (with -> meaning import) *A -> B -> C*: if the last two have a template matching a node, only the template from B will be applied, because it has a higher import precedence. Now suppose this chain *A -> B, C*: if the last two have a template marching a node (with the same calculated priority) it would be an error, but by error recovery mechanism, processor could apply the template declared lastly.
Alejandro
Alright, Whats the best way of combining these transformations then?
Ace
@Ace: It depends of use case. Think of this like about class inheritance.
Alejandro
Well all I need is to run docbook2dita.xsl first, then 8 other xsl after that to fix the artifacts and stuff. The order of the 8 xsl don't matter
Ace
A: 

It is possible to apply two or more transformations in sequence (chain), each operating on the result of the previous one.

Here is how to chain together two transformations in XSLT 2.0 (the XSLT 1.0 example is similar, but one needs to use the xxx:node-set() function on each intermediary result):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vPass1">
  <xsl:apply-templates select="/*"/>
 </xsl:variable>

 <xsl:template match="node()|@*" name="id" mode="#default pass2">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*" mode="#current"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:apply-templates select="$vPass1" mode="pass2"/>
 </xsl:template>

 <xsl:template match="num/text()">
  <xsl:value-of select="2*."/>
 </xsl:template>

 <xsl:template match="num/text()" mode="pass2">
  <xsl:value-of select="1+."/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<nums>
 <num>1</num>
 <num>2</num>
 <num>3</num>
 <num>4</num>
 <num>5</num>
</nums>

two transformations are applied in a sequence. The result of the first transformation is a new XML document in which the value of each num/text() node is twice the value of the original num/text() node. The second transformation is applied on this intermediary result and its action is to add 1 to every num/text() node and to produce this result as the new corresponding `num/text()' node.

Thus the comulative result is that every $n (the value of every num/text() node) is transformed to 2*$n+1 :

<nums>
   <num>3</num>
   <num>5</num>
   <num>7</num>
   <num>9</num>
   <num>11</num>
</nums>
Dimitre Novatchev