NYCPHP Meetup

NYPHP.org

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

Joelle Tegwen tegwe002 at umn.edu
Wed Mar 11 14:17:22 EDT 2009


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
>



More information about the talk mailing list