Using xslt_process on data from Tamino

Hi!

I tried to do an xslt_process on a nodeset that I got from an X-Query.

I tried this:
__________

if (!($tamino->query("/person")))
thfPrintError($tamino);
else
{
$domnodeResultElement = $tamino->getResultDomNode();
if ($domnodeResultElement) {
$domnodeCurrent = $domnodeResultElement->first_child();
}
}
$xh = xslt_create();
$result = xslt_process($xh, “…\Program Files\Apache Group\Apache\htdocs\t$domnodeResultElement” , ‘b.xsl’);


But I got the error:
Schema defined successfully
--------------------------------------------------------------------------------
Data with encoding “windows-1252” loaded successfully
--------------------------------------------------------------------------------

Warning: Sablotron error on line 32: cannot open file ‘c:/Program Files/Apache Group/Apache/htdocs $domnodeResultElement’ in c:\program files\apache group\apache\htdocs\t\t2.php on line 64
Sorry, sample.xml could not be transformed by sample.xsl into the $result variable the reason is that cannot open file ‘c:/Program Files/Apache Group/Apache/htdocs $domnodeResultElement’ and the error code is 4


It seems that xslt_process only accepts a file??

When I skipped the path (c:…\t) Sablotron searhed in c:\php4

Can anyone help me with what to do to get the result of the query transformed with xslt?

Any help is appreciated!

Thanks in advance!

Best regards,
Tom-Erik Valso

Hello there.

It 's been a while since I have not grasped my hands on PHP code. Anyway, I will try to help you out on this one.

You are right : “xslt_process” expects xml / xsl containers (2nd and 3rd parameters) as files.
But … you can manage to give parameters (such as your XML DOM from Tamino) to this function. You have to serialize your DOM to String. Then you set the XML document as an argument.

Here comes a snippet in order to perform your task :


if (!($tamino->query("/person")))
{
fPrintError($tamino);
}
else
{
$domnodeResultElement = $tamino->getResultDomNode();
if ($domnodeResultElement)
{
$domnodeCurrent = $domnodeResultElement->first_child();
// Serialize DOM → String …
$xml = domxml_dumpmem($domnodeCurrent);
//check domxml_dumpmem syntax, this may have change to dump_mem(indent, encoding) e.g. :
//$xml = $domnodeCurrent->dump_mem(false, “ISO-8859-1”)
$arguments = array(’/_xml’ => $xml);
$xh = xslt_create();
$result = xslt_process($xh, “arg:/_xml”, “b.xsl”, NULL, $arguments);
// DEBUG
echo $result
}
}


Honestly, I have not tested this. Tell me if it either works or not.
Thanks.

Cheers.

Bertrand Martel
Software AG France

Hi!

Thanks for the advice!
I will for sure test it out and give you the result!

I guess I’ve tested it out by thursday :slight_smile:

Best regards,
Tom-Erik

Hi again!

Yes, that worked! :slight_smile:

Thanks!!

I was only on thing I needed to do:
I skipped the statement:

$domnodeCurrent = $domnodeResultElement->first_child();

Here is the code one one hack:


if (!($tamino->query("/person")))
thfPrintError($tamino);
else
{
$domnodeResultElement = $tamino->getResultDomNode();
if ($domnodeResultElement) {

$kundeid = “222222”;
$xml = domxml_dumpmem($domnodeResultElement);
$xml = str_replace(‘ino:id=“1”’,’’,$xml);

$arguments = array(’/_xml’ => $xml);
$xh = xslt_create();
$result = xslt_process($xh, “arg:/_xml”, “b.xsl”, NULL, $arguments);
// DEBUG
// echo $result

}
}

if ($result) {
// echo “SUCCESS, sample.xml was transformed by sample.xsl into the $result”;
// echo " variable, the $result variable has the following contents\n
\n";
// echo “

\n”;
echo $result;
// echo “
\n”;
}
else
{
echo “Sorry, sample.xml could not be transformed by xsl into”;
echo " the $result variable the reason is that " . xslt_error($xh);
echo " and the error code is " . xslt_errno($xh);
}

xslt_free($xh);
// undefine collection
if (!($tamino->undefine(“myAddresses2”)))
thfPrintError($tamino);
else
// echo “Schema undefined successfully\n”;
// echo “<hr size=“1” />\n”;

?>



I didn’t get the namespaces to work for when not replacing ino I got the error:

Sorry, sample.xml could not be transformed by xsl into the $result variable the reason is that invalid namespace prefix ‘ino’ and the error code is 24


I didn’t understand what namespace to use in my xsl file.
Here is the xsl file:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version=“1.0"
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” xmlns:ino=“http://heim.ifi.uio.no/~tomerikv”><xsl:template match=”/">


Welcome to ANP Booking Activity 3 of 4

This page is English


















<xsl:for-each select=“person/per”>













</xsl:for-each>
ACTIVITY ACT 2 ACTIVITY START ACTIVITY STOP DESC DIFFICULITY PRICE BOOK HOMEPAGE
<xsl:value-of select=“actid”/> <xsl:value-of select=“actid2”/> <xsl:value-of select=“start”/> <xsl:value-of select=“stop”/> <xsl:value-of select=“desc”/> <xsl:value-of select=“difficulity”/> <xsl:value-of select=“price”/> bookingfield <xsl:value-of select=“homepage”/>





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



Do you know what namespace to use?

The root element has the prefix ino:

Anyway, I’m very happy that it now works!!

Thanks a lot again!!

Best regards,
Tom-Erik

Hi again!

I forgot the write what I did with the ino prefix.

I replaced it in the string with str_replace,
and that is obviously not the best thing to do.

:slight_smile:

Best regards,
Tom-Erik

Hello Tom-Eric,

You should (have to) use the URI defined by Software AG to declare namespaces.
Those are the following :
ino => http://namespaces.softwareag.com/tamino/response2
xql => http://metalab.unc.edu/xql/

Hence, in your XSL you have to declare those NS.

Your XSL should start with :



<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version=“1.0"
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform"
xmlns:ino=“http://namespaces.softwareag.com/tamino/response2” xmlns:xql=“http://metalab.unc.edu/xql/”>

<xsl:template match=”/”>

[…]



Your XML returned from Tamino may (I’m not sure about this, it all depends on Tamino PHP API which I do not masterize) look like the following :

<ino:response …>
xql:queryyour X-QUERY</xql:query>
<ino:message ino:returnvalue=“0”>
ino:messagelineXQL Request processing</ino:messageline>
</ino:message>
xql:result

kim
deal



</xql:result>
</ino:response>

I would recommend to write the following XSL templates



<xsl:template match="/">
<HTML CODE … TABLE HEADER …>
<xsl:apply-templates select="/ino:response/xql:result/person" />
<HTML CODE … TABLE FOOTER …>
</xsl:template>

<xsl:template match=“person”>

<xsl:value-of select=“actid”/>
<xsl:value-of select=“actid2”/>
<xsl:value-of select=“start”/>
<xsl:value-of select=“stop”/>
<xsl:value-of select=“desc”/>
<xsl:value-of select=“difficulity”/>
<xsl:value-of select=“price”/>
bookingfield
<xsl:value-of select=“homepage”/>

</xsl:template>





Defining a specific “person” template is cleaner and be reusable.
But this is just an advice.

Cheers.

Bertrand Martel
Software AG France