1

I have some XML, a XSL file and 4 CSS files. I am trying to process the XML using XSLTProcessor but it isn't working.



    $xml = new DomDocument;
    $xml->load('label.xml');

    $xsl = new DomDocument;
    $xsl->load('HTMLRestOfWorldRoutingLabelRendererOrig.xsl');

    $proc = new xsltprocessor;
    $proc->importStyleSheet($xsl);
    $result = $proc->transformToXML($xml); 

    echo $result;


But this just displays the below without any sort of formatting.

<?xml version="1.0"?>



            1
            1.11kg
            piece1
            1100123456782011641024001011
            123456782|123456782||1||John Smith|TNT Express|ATHERSTONE|CV9 1TT|GB|S||TNT Corporate Head Office|Neptunusstraat 41-63|AMSTERDAM|1011 AA|NL||EX|N|PR||||0|12.34|GBP|N|piecelinegoods desc|3|1.11|1.3676310000000003|N|18 Jan 2012|13:51:00


            2
            1.11kg
            piece1
            1100123456782021641024001011
            123456782|123456782||2||John Smith|TNT Express|ATHERSTONE|CV9 1TT|GB|S||TNT Corporate Head Office|Neptunusstraat 41-63|AMSTERDAM|1011 AA|NL||EX|N|PR||||0|12.34|GBP|N|piecelinegoods desc|3|1.11|1.3676310000000003|N|18 Jan 2012|13:51:00


            3
            1.11kg
            piece3
            1100123456782031641024001011
            123456782|123456782||3||John Smith|TNT Express|ATHERSTONE|CV9 1TT|GB|S||TNT Corporate Head Office|Neptunusstraat 41-63|AMSTERDAM|1011 AA|NL||EX|N|PR||||0|12.34|GBP|N|piecelinegoods desc|3|1.11|1.3676310000000003|N|18 Jan 2012|13:51:00


            123456782

                John Smith
                TNT Express
                TNT House
                ATHERSTONE
                Warks
                CV9 1TT
                GB


                TNT Corporate Head Office
                Neptunusstraat 41-63
                2132 JA Hoofddorp
                AMSTERDAM

                1011 AA
                NL


                100445
                GB

            3
            Express
            PR
            2012-01-18
            INT
            AIR
            C
            2


                CVT



                    EMA


                    LGG



                SP8
                19
                2012-01-19


            01

            GBP 12.34

            BSH

My label.xml is at http://pastebin.com/Shm09jCK

I have uploaded HTMLRestOfWorldRoutingLabelRendererOrig.xsl to http://pastebin.com/QPXE3B0r

I must be missing something obvious but this is new to me and I am not sure!

2
  • No errors from DomDocument class? Commented Jan 18, 2012 at 14:15
  • Hi, no, seems fine. <pre> <code> $xml = new DomDocument; if($xml->load('label.xml')) { echo "Loaded OK"; } else { echo "Loading failed"; } $xsl = new DomDocument; if($xsl->load('HTMLRestOfWorldRoutingLabelRendererOrig.xsl')) { echo "Loaded OK"; } else { echo "Loading failed"; } </code> </pre> Shows "Loaded OK" both times Commented Jan 18, 2012 at 14:21

2 Answers 2

2

In your XSLT code there isn't any template that has a match attribute. This means that none of the templates are executed and the XSLT processor applies the built-in (default) templates for every node type.

The net result from applying the built-in templates is that the output is a concatenation of all text nodes in the source XML document -- which is exactly what you get.

Solution:

Define at least one matching template such as <xsl:template match="/">. Within the code of this template you may call your named templates, though calling templates is generally a bad practice and applying templates should generally be preferred.

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting, this seems to almost be perfect. If I use <xsl:template match="/"> then I see one label but with the actual content missing, if I use <xsl:template match="labelResponse/"> then I see three labels but with the content missing. If I remove the ../ references throughout the XSL then I see three labels with the content correctly showing on the 2nd but not the 1st or 3rd. I think this is enough for me to be getting on with now, I will try and figure out what is happening. Thank you - you deserve one of these - i.imgur.com/58dmS.gif
0

Try:

<xsl:output method="html" />

6 Comments

I have tried adding this to the XSL file but it doesn't seem to make a difference.
Try this: $xml = new DomDocument; $xml->load('label.xml'); $xsl = new SimpleXMLElement('HTMLRestOfWorldRoutingLabelRendererOrig.xsl'); $proc = new xsltprocessor; $proc->importStyleSheet($xsl); $result = $proc->transformToXML($xml); echo $result;
Thank you for your help. That gives me a PHP error: Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '<' not found in /Users/chris/Dropbox/Sites/tnt/3/label.php on line 8 Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: HTMLRestOfWorldRoutingLabelRendererOrig.xsl in /Users/chris/Dropbox/Sites/tnt/3/label.php on line 8 Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /Users/chris/Dropbox/Sites/tnt/3/label.php on line 8
If I change the SimpleXMLElement line to: $xsl = new SimpleXMLElement(file_get_contents('HTMLRestOfWorldRoutingLabelRendererOrig.xsl')); Then I get no errors but the output is the same as it was before - basically just the XML with all the tags stripped out
I tried this earlier, I get "SUCCESS, sample.xml was transformed by sample.xsl into the $result variable, the $result variable has the following contents" and then I see below exactly the same as the above example. Just the original XML with the tags stripped out!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.