简体   繁体   中英

Retrieve xsl:variable in xml using PHP

Note.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="./xsl/avsdk.xsl"?>

<Table CreatedOn="Tue Nov 17 22:30:53 PST 2015" Name="Hello" Version="3.6.10294.2" TimeStamp="1447828253">
.....
.....
</Table>

Previoulsy we have used the above xml file and used the below PHP command to retrieve the value for Table tag Name and Version, and processed the Table data using Xml object to convert it to other format.

PHP:

$xml = simplexml_load_file("note.xml")
$table_attrs = $xml->attributes();
if( 0 === strpos( $table_attrs["Name"], "Hello" ) )
{
.....
}
else if( 0 === strpos( $table_attrs["Name"], "Hai" ) )
{
.....
}

Now the XML file format changed like below. So i have changed the command like below(ie $xml->Table[0]->attributes()) to take the Table tag Name and version, and store the entire table content as a XML object to convert it to other format. when i tried to run the below PHP command, it throws the error "Fatal error: Call to a member function attributes() on null". Please let me know the command to take Table Name and version, and how to store the entire Table content as a xml object

Note.xml

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="#sc_stylesheet"?>
<!DOCTYPE supportchart [<!ATTLIST xsl:stylesheet id ID #REQUIRED>]>

<xsl:stylesheet id="sc_stylesheet" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" omit-xml-declaration="yes"/>
    <xsl:variable name="support_chart">
        <supportchart>
            <Table CreatedOn="Wed Feb 03 15:52:32 2016" Name="Hello" Version="4.2.326.0" Timestamp="1454543552.61">
            .....
            .....
            </Table>
        </supportchart>
    </xsl:variable> 
    <xsl:template match="Table">
    ....
    ....
    <xsl:template match="xsl:template"> </xsl:template>
</xsl:stylesheet>

PHP:

$xml = simplexml_load_file("note.xml")
$table_attrs = $xml->Table[0]->attributes();
if( 0 === strpos( $table_attrs["Name"], "Hello" ) )
{
.....
}
else if( 0 === strpos( $table_attrs["Name"], "Hai" ) )
{
.....
}

As mentioned, XSLT is a well-formed XML document that most parsers should handle but usually one would not parse a script file for data ( akin to parsing a PHP script for MySQL data ). But since actual values are used in variable definition of XSLT, consider using SimpleXMLElement and then use its xpath() method to parse your needed attributes. In XPath queries, attributes require @ in its referencing but not needed for node values:

$data = file_get_contents("$note.xml");
$xml = new SimpleXmlElement($data);

$nameArray = $xml->xpath("//Table/@Name");
$versionArray = $xml->xpath("//Table/@Version");
$timestampArray = $xml->xpath("//Table/@Timestamp");

Should you need to parse node or attribute values enclosed in a namespace prefix, here being xsl , you will need to register the xsl namespace first:

$data = file_get_contents("$note.xml");
$xml = new SimpleXmlElement($data);

$xml->registerXPathNamespace('xsl', 'http://www.w3.org/1999/XSL/Transform');

$variableArray = $xml->xpath("//xsl:variable/@name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM