NYCPHP Meetup

NYPHP.org

[nycphp-talk] Transforming XML with XSL (XsltProcessor problems)

Joelle Tegwen tegwe002 at umn.edu
Wed Mar 11 15:31:42 EDT 2009


It's not quite where I would like it to be, but here it is. I included 
my custom Exception class, but it extends another, which I did not 
include. So you'll want to do something with that first.

Improvements welcome. :)

Hope this helps.
Joelle

Ben Sgro wrote:
> Hey Joelle,
>
> Thanks - care to share the class?
>
> I'll reconsider the XSL - thanks.
>
> - Ben
>
> Joelle Tegwen wrote:
>> Yeah, I've found this to be fairly cumbersome. I use it a lot so I 
>> wrote a class that wraps this functionality. I can pass in any kind 
>> of xml (DomDocument, SimpleXMLElement, string, file locaiton, etc) 
>> and it just handles it all for me.
>>
>> BTW - unless you need it // is relatively expensive in xslt. You 
>> should use relative paths whenever possible.
>>
>> Glad you figured it out.
>> Joelle
>>
>> Ben Sgro wrote:
>>> Thanks for the feedback
>>>
>>> The error was super simple:
>>>
>>>        $xml = new DOMDocument;
>>>        $xml->loadXML($this->_payload);
>>>
>>>        $xsl = new DOMDocument;
>>>        $xsl->load($this->_filePath);
>>>
>>>        $proc = new XSLTProcessor;
>>>        $proc->importStyleSheet($xsl);
>>>              return $proc->transformToXML($xml);   I was calling 
>>> load(STRING) vs loadXML(STRING), so when it attempting to transform 
>>> the XML, it (the dom doc) was empty - it didn't load the xml string 
>>> data.
>>> Too bad it doesn't throw an error or exception if an argument is 
>>> incorrect.
>>>
>>> - Ben
>>>
>>> Joelle Tegwen wrote:
>>>> This worked for me.  PHP 5.2.6-2ubuntu4.1 with Suhosin-Patch 0.9.6.2
>>>>
>>>>        $xml =<<<XML
>>>> <ProfileRequest templateId="1" submitUser="asdf" 
>>>> submitDateTime="2009-01-12T17:32:46">
>>>>   <DataItem name="AccountNumber" value="600978"/>
>>>> </ProfileRequest>
>>>> XML;
>>>>
>>>>        $xsl =<<<XSL
>>>> <xsl:stylesheet
>>>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
>>>> xmlns="http://www.w3.org/1999/xhtml">
>>>> <xsl:template match="/ProfileRequest">
>>>> <Data>
>>>> <AccountNumber>
>>>>    <xsl:value-of select="DataItem[@name='AccountNumber']/@value"/>
>>>> </AccountNumber>
>>>> </Data>
>>>> </xsl:template>
>>>> </xsl:stylesheet>
>>>> XSL;
>>>>
>>>>
>>>> Ben Sgro wrote:
>>>>> Hello,
>>>>>
>>>>> My PHP code is:
>>>>> ---------------------
>>>>>
>>>>>        $xml = new DOMDocument;
>>>>>        $xml->load($this->_payload);                $xsltProcessor 
>>>>> = new XsltProcessor();
>>>>>        $xsl = new DomDocument;
>>>>>        $xsl->load('../lib/Transformations/text.xsl');
>>>>>              $xsltProcessor->importStylesheet($xsl);
>>>>>        $result = $xsltProcessor->transformToXml($xml);
>>>>>          return $result;
>>>>>
>>>>>
>>>>> My XSL is:
>>>>> --------------
>>>>>
>>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>>> <xsl:stylesheet
>>>>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
>>>>> xmlns="http://www.w3.org/1999/xhtml"
>>>>> >
>>>>> <xsl:template match="/">
>>>>> <xsl:element name="Data">
>>>>> <xsl:element name="AccountNumber"><xsl:value-of 
>>>>> select="//DataItem[@name='AccountNumber']/@value"/>
>>>>> </xsl:element>
>>>>> </xsl:element>
>>>>> </xsl:template>
>>>>> </xsl:stylesheet>
>>>>>
>>>>>
>>>>> And my XML is:
>>>>> ---------------------
>>>>>
>>>>> <ProfileRequest templateId="1" submitUser="asdf" 
>>>>> submitDateTime="2009-01-12T17:32:46">
>>>>>    <DataItem name="AccountNumber" value="600978"/>
>>>>> </ProfileRequest>
>>>>>
>>>>>
>>>>>
>>>>> The $result is:
>>>>> -----------------
>>>>>
>>>>> <?xml version="1.0"?>
>>>>> <Data 
>>>>> xmlns="http://www.w3.org/1999/xhtml"><AccountNumber></AccountNumber></Data> 
>>>>>
>>>>>
>>>>> So, as you can see the AccountNumber is missing. This XSL/XML 
>>>>> works fine in Author (XML Tool).
>>>>> Any ideas why this would fail? Also, the Xpath 
>>>>> (//DataItem[@name='AccountNumber']/@value) works
>>>>> if I make the XML a simpleXML object and call xpath on it.
>>>>>
>>>>> Thanks for your help!
>>>>>
>>>>> - Ben
>>>>> _______________________________________________
>>>>> New York PHP User Group Community Talk Mailing List
>>>>> http://lists.nyphp.org/mailman/listinfo/talk
>>>>>
>>>>> http://www.nyphp.org/show_participation.php
>>>>>
>>>> _______________________________________________
>>>> New York PHP User Group Community Talk Mailing List
>>>> http://lists.nyphp.org/mailman/listinfo/talk
>>>>
>>>> http://www.nyphp.org/show_participation.php
>>>>
>>> _______________________________________________
>>> New York PHP User Group Community Talk Mailing List
>>> http://lists.nyphp.org/mailman/listinfo/talk
>>>
>>> http://www.nyphp.org/show_participation.php
>>>
>> _______________________________________________
>> New York PHP User Group Community Talk Mailing List
>> http://lists.nyphp.org/mailman/listinfo/talk
>>
>> http://www.nyphp.org/show_participation.php
>>
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show_participation.php
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: XMLTransformation.zip
Type: application/zip
Size: 2323 bytes
Desc: not available
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20090311/5ee0b498/attachment.zip>


More information about the talk mailing list