Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the document() function work in XSLT?

so basically I have been tearing my hair out trying to get the document() function in xslt working, however I cannot find a way for my life. I have been told to learn it in Notepad++ using the XMLTools plugin and transforming the xml.

I have created 2 basic xml files with brief data in but I am incapable of even linking these two files together as I cannot find any help online for learning this.

My first xml named cars.xml:

<?xml version="1.0"?>
<!DOCTYPE cars SYSTEM "cars.dtd">
<cars>
    <car>
        <carManufacturer>BMW</carManufacturer>
        <carModel>1 Series</carModel>
        <carYear>2013</carYear>
        <carPrice>£7,950</carPrice>
    </car>
    <car>
        <carManufacturer>BMW</carManufacturer>
        <carModel>3 Series</carModel>
        <carYear>2014</carYear>
        <carPrice>£9,950</carPrice>
    </car>
</cars>

And my second xml file named customers.xml:

<?xml version="1.0"?>
<!DOCTYPE customers SYSTEM "cars.dtd">
<customers>
    <customer>
        <firstName>John</firstName>
        <lastName>Smith</lastName>
        <location>Carterton</location>
    </customer>
    <customer>
        <firstName>Jeremy</firstName>
        <lastName>Clarkson</lastName>
        <location>Chipping Norton</location>
    </customer>
</customers>

This is what I have come up with in my xsl soo far but I have gotten no where:

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

    <xsl:output method="html" indent="no"/>
    <xsl:template match="/">
        <xsl:call-template name="StartHTML">
            <xsl:with-param name="Title" select="'Cars'"/>
        </xsl:call-template>
    </xsl:template>
    <!--<xsl:template match="cars">
        <xsl:variable name="CarName">
            <xsl:value-of select="car/carManufacturer"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="car/carModel"/>
        </xsl:variable>

    </xsl:template>-->
</xsl:stylesheet>

Any help at all using this function would be greatly appreciated :)

like image 627
Lukeyy Avatar asked Aug 14 '26 15:08

Lukeyy


1 Answers

From your question it's not clear how your two files are linked...

The document function can simply be used like this:

<xsl:variable name="carsFile" select="document('cars.xsml')"/>

And then for example can be used as in:

<xsl:value-of select="$carsFile/cars/car/carManufacturer"/>

Also if you don't have access to an XML/XSLT development environment (XMLSpy, Oxygen XML, ...) which are expensive for users that don't need them often, from my experience, the XMLTools plugin in Notepad++ was hard to work with and error messages not always easy to decode.

I would suggest you keep editing your XSLT in Notepad++ and have a simple batch file from which you run Saxon on your files, something like this:

java -cp saxon9he.jar net.sf.saxon.Transform -t -s:input.xml -xsl:transform.xsl -o:output.xml
like image 67
Sebastien Avatar answered Aug 16 '26 19:08

Sebastien