我遇到的问题是XSLT如何将参数传递给我的PHP函数。我正在使用<xsl:value-of select="php:function('form::validate_add',@name,type,message)" />
将属性名称,元素类型和元素消息传递给php函数,但传递的参数是大型数组,包括我需要的无用信息。
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);
}
返回:
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
)
)
所以看起来数组的“value”键是我追求的正确值。有没有办法直接传递这个参数而不是传递整个数组并且必须从PHP函数中的数组中提取正确的值?我宁愿不改变我的PHP功能或添加新功能。
答案 0 :(得分:6)
如果要从XPath / XSLT传递DOM节点的字符串值,请使用<xsl:value-of select="php:function('form::validate_add', string(@name), string(type), string(message))" />
答案 1 :(得分:0)
<xsl:value-of select="php:function ('ucfirst', string(path/element/text()))"/>
无论如何,希望对某人有用......