Question

I'm using XSLT4Node, but, in the documentation I didnt find about how I can add more childs to the node. Someone has an idea about it?

Just now I have:

order.xml

<order><book ISBN="10-861003-324"><title>The Handmaid's Tale</title><price>19.95</price></book><cd ISBN="2-3631-4"><title>Americana</title><price>16.95</price>
  </cd>
</order>

discount.xml

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="discount"/>
    <xsl:template match="/">
        <order>
            <xsl:variable name="sub-total" select="sum(//price)"/>
            <total>


    <xsl:value-of select="$sub-total"/>
        </total>15% discount if paid by: <xsl:value-of select="$discount"/>
    </order>
</xsl:template>

index.js

var xslt4node = require('xslt4node');

var config = {
        xsltPath: 'discount.xsl',
        sourcePath: 'order.xml',
        result: 'result.xml',
        params: {
            adiscount: '2014/08/02'
        },
        props: {
            indent: 'yes'
        }
    };

xslt4node.transform(config, function (err) {
            if (err) {
                console.log(err);
            }
            finishRequest(  response , "done" );
        });
Was it helpful?

Solution

Is possible, the library allow insert multiple parameters and use the xls functions like: xsl:for-each select="catalog/cd" tag, to insert multiple values with a loop

index.js

var xslt4node = require('xslt4node');

var config = {
        xsltPath: 'discount.xsl',
        sourcePath: 'order.xml',
        result: 'result.xml',
        props: {
            indent: 'yes'
        }
    };

discount.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

    <order>
        <xsl:for-each select="catalog/cd">
            <albums>
                <title>
                    <xsl:value-of select="title"/>
                </title>
                <artist>
                    <xsl:value-of select="artist"/>
                </artist>
            </albums>
        </xsl:for-each>
    </order>


</xsl:template>

order.xml

<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top