Devon Devon - 28 days ago 13
PHP Question

php:function in XSLT to pass arguments directly, not in an array

I am having issues with how XSLT is passing arguments to my PHP function. I am using

<xsl:value-of select="php:function('form::validate_add',@name,type,message)" />
to pass the attribute name, element type, and element message to a php function but the arguments being passed are large arrays including useless information for my needs.

XML:

<element name='text2-1'>
<type>required</type>
<message>The field Enter Text is required</message>
</element>
<element name='textarea'>
<type>required</type>
<message>The textarea is required</message>
</element>


XSLT:

<xsl:for-each select="element">
<xsl:value-of select="php:function('form::validate_add',@name,type,message)" />
</xsl:for-each>


PHP:

public static function validate_add($name, $type, $message=NULL) {
#tmp
print_r($name);
}


Returning:

Array
(
[0] => DOMAttr Object
(
[name] => name
[specified] => 1
[value] => text2-1
[ownerElement] => (object value omitted)
[schemaTypeInfo] =>
[nodeName] => name
[nodeValue] => text2-1
[nodeType] => 2
[parentNode] => (object value omitted)
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] =>
[attributes] =>
[ownerDocument] => (object value omitted)
[namespaceURI] =>
[prefix] =>
[localName] => name
[baseURI] =>
[textContent] => text2-1
)

)


So it looks like the "value" key of the array is the correct value that I am after. Is there any way to pass this argument directly rather than passing the entire array and having to extract the correct value from the array in the PHP function? I would prefer not to alter my php function or add a new function.

Answer

If you want to pass string values of the DOM nodes from XPath/XSLT then use <xsl:value-of select="php:function('form::validate_add', string(@name), string(type), string(message))" />

Comments