开发者

Combining XSL documents

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?


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">
    <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">
    <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>


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">
 <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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜