9
Loading methods in PHP DOMDocument class http://php.net/manual/en/class.domdocument.php DOMDocument extends DOMNode { . . . mixed load ( string $filename [, int $options = 0 ] ) mixed loadXML ( string $source [, int $options = 0 ] ) bool loadHTML ( string $source ) bool loadHTMLFile ( string $filename ) . . . } We have seen that load() loads XML from a file loadXML() loads XML from a string loadHTML() loads HTML from a string Unlike loading XML, HTML does not have to be well-formed to load load HTMLFile() loads HTML from a string Unlike loading XML, HTML does not have to be well-formed to load

Loading methods in PHP DOMDocument class DOMDocument extends DOMNode {... mixed load ( string $filename

Embed Size (px)

Citation preview

Page 1: Loading methods in PHP DOMDocument class  DOMDocument extends DOMNode {... mixed load ( string $filename

Loading methods in PHP DOMDocument class• http://php.net/manual/en/class.domdocument.php

DOMDocument extends DOMNode {

. . .

mixed load ( string $filename [, int $options = 0 ] )

mixed loadXML ( string $source [, int $options = 0 ] )

bool loadHTML ( string $source )

bool loadHTMLFile ( string $filename )

. . .

}

• We have seen that load() loads XML from a file• loadXML() loads XML from a string• loadHTML() loads HTML from a string

– Unlike loading XML, HTML does not have to be well-formed to load

• load HTMLFile() loads HTML from a string– Unlike loading XML, HTML does not have to be well-formed to load

Page 2: Loading methods in PHP DOMDocument class  DOMDocument extends DOMNode {... mixed load ( string $filename

XSLTProcessor class in PHP• http://php.net/manual/en/class.xsltprocessor.php

XSLTProcessor {

string getParameter ( string $namespaceURI , string $localName )

bool hasExsltSupport ( void )

void importStylesheet ( object $stylesheet )

void registerPHPFunctions ([ mixed $restrict ] )

bool removeParameter ( string $namespaceURI , string $localName )

bool setParameter ( string $namespace , string $name , string $value )

bool setProfiling ( string $filename )

string transformToXML ( DOMDocument $doc )

DOMDocument transformToDoc ( DOMNode $doc )

int transformToURI ( DOMDocument $doc , string $uri )

}

• We have already used the blue methods above

Page 3: Loading methods in PHP DOMDocument class  DOMDocument extends DOMNode {... mixed load ( string $filename

transform methods in PHP XSLTProcessor class

XSLTProcessor {

. . .

string transformToXML ( DOMDocument $doc )

DOMDocument transformToDoc ( DOMNode $doc )

int transformToUri ( DOMDocument $doc , string $uri )

}

• transformToXML() transforms the source DOMDocument to a string• transformToDoc() transforms the source DOMDocument to a

DOMDocument• transformToUri() transforms the source DOMDocument to the URI

specified in the string– For example,

$processor->transformToUri($xmldoc,'output.html');

places the output from the transformation in a file called output.html

Page 4: Loading methods in PHP DOMDocument class  DOMDocument extends DOMNode {... mixed load ( string $filename

Global parameters for XSL stylesheets• XSLT allows parameters whose scope is the entire

stylesheet– Default values can be set for these parameters– External values can be passed to these parameters– If external values are passed, these override the default values

• The values of these parameters can then control the form of processing performed by the stylesheet

Page 5: Loading methods in PHP DOMDocument class  DOMDocument extends DOMNode {... mixed load ( string $filename

Global parameters in XSLT, Example• Stylesheet below has a global parameter, called infoWanted, to

which is gives a default value that can be over-ridden by an externally-set value

• Depending on value of parameter, stylesheet outputs either the names or the ages of the people in the XML document which is being processed

<?xml version="1.0"?>

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >

<xsl:param name="infoWanted" select="ages"/>

<xsl:template match="/people">

<xsl:choose>

<xsl:when test="$infoWanted='names'">

<names> <xsl:copy-of select="person/name"/> </names>

</xsl:when>

<xsl:otherwise>

<ages> <xsl:copy-of select="person/age"/> </ages>

</xsl:otherwise>

</xsl:choose>

</xsl:template>

</xsl:transform>

Page 6: Loading methods in PHP DOMDocument class  DOMDocument extends DOMNode {... mixed load ( string $filename

Global parameters in XSLT, Example

• Suppose stylesheet on previous slide is in demo304.xsl

• demo304.php applied it to the XML document in demo304.xml

• Different output is given, depending on value of query string given to demo304.php

Page 7: Loading methods in PHP DOMDocument class  DOMDocument extends DOMNode {... mixed load ( string $filename

parameter methods in PHP XSLTProcessor class

XSLTProcessor {

. . .

bool setParameter ( string $namespace , string $name , string $value )

string getParameter ( string $namespaceURI , string $localName )

bool removeParameter ( string $namespaceURI , string $localName )

}

• setParameter() sets the value of parameter to be used in subsequent transformations; if the parameter doesn't exist in stylesheet, it will be ignored

• getParameter() gets a parameter if previously set by setParameter()• removeParameter() removes a parameter, if set; this will make the

processor use default value for the parameter as specified in the stylesheet

Page 8: Loading methods in PHP DOMDocument class  DOMDocument extends DOMNode {... mixed load ( string $filename

Profiling

• An XSLT profiling tool determines – which templates were called– how many times– how much time was spent executing them

• This information may help in re-writing the stylesheet to improve its speed of execution

Page 9: Loading methods in PHP DOMDocument class  DOMDocument extends DOMNode {... mixed load ( string $filename

profiling method in PHP XSLTProcessor class

XSLTProcessor {

. . .

bool setProfiling ( string $filename )

. . .

}

• setProfiling() specifies the file to which the profiling information should be written when a stylesheet is executed

• Example

$proc->setProfiling('profiling.txt');

specifies that profiling results will be written to the file called profiling.txt