Skip to content Skip to sidebar Skip to footer

Pull Specific Xml Node(s) Into Html Document

first time poster. Please be gentle. This topic may be similar to several other questions out there, but as far as I can see this is a unique problem I'm trying to solve. FWIW, I'

Solution 1:

Sounds like you are trying to do a transformation from XML to HTML. For this you can use a technology called XSLT but commonly known by many as an xslt transformation. You can use xslt in conjunction with xpath (query language for xml) to do exactly what you describe in your question.

Here is the xml: (added the div id's to the xml)

<?xml version="1.0" encoding="utf-8"?><section><page><divid="greenBackground"></div><nameimage="image1.jpg">Section One</name><copy>This is a description of section one.</copy></page><page><divid="blueBackground"></div><nameimage="image2.jpg">Section Two</name><copy>This is a description of section two.</copy></page></section>

Here is the xslt:

<?xml version="1.0" encoding="iso-8859-1"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:templatematch="/"><html><body><xsl:for-eachselect="section/page"><div><xsl:attributename="id"><xsl:value-ofselect="div//@id"/></xsl:attribute><h1><xsl:value-ofselect="name"/></h1><p><xsl:value-ofselect="copy"/></p><img><xsl:attributename="src"><xsl:value-ofselect="name//@image"/></xsl:attribute></img></div></xsl:for-each></body></html></xsl:template></xsl:stylesheet>

Here is the output after you run the tranform:

<html><body><divid="greenBackground"><h1>Section One</h1><p>This is a description of section one.</p><imgsrc="image1.jpg"></div><divid="blueBackground"><h1>Section Two</h1><p>This is a description of section two.</p><imgsrc="image2.jpg"></div></body></html>

Enjoy!

Solution 2:

Here's another version. I find it much cleaner if you:

  • avoid <xsl:for-each/> (using XSLT's pattern matching instead is more idiomatic)
  • use attribute value templates (these are the attribute="{..}")
  • you should also set <xsl:output/> since you're generating HTML (I have assumed 4.0)

I assume you can add an extra attribute id on your <page/> elements in the input (otherwise there's nowhere to get your id from!)

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:outputmethod="html"version="4.0"indent="yes"/><xsl:templatematch="/"><html><body><xsl:apply-templates/></body></html></xsl:template><xsl:templatematch="page"><divid="{@id}"><h1><xsl:value-ofselect="name"/></h1><p><xsl:value-ofselect="copy"/></p><imgsrc="{name/@image}"/></div></xsl:template></xsl:stylesheet>

Solution 3:

Change your XML to this (to set the div id names):

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="web_page.xsl"?><section><page><div_id>greenBackground</div_id><nameimage="image1.jpg">Section One</name><copy>This is a description of section one.</copy></page><page><div_id>blueBackground</div_id><nameimage="image2.jpg">Section Two</name><copy>This is a description of section two.</copy></page>

This XSL will translate your XML just the way you asked:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:templatematch="/"><html><body><xsl:for-eachselect="section/page"><div><xsl:attributename="id"><xsl:value-ofselect="div_id"/></xsl:attribute><h1><xsl:value-ofselect="name"/></h1><p><xsl:value-ofselect="copy"/></p><img><xsl:attributename="src"><xsl:value-ofselect="name//@image"/></xsl:attribute></img></div></xsl:for-each></body></html></xsl:template></xsl:stylesheet>

Solution 4:

This is how to get the transformation done with PHP:

$proc=new XsltProcessor;  
$proc->importStylesheet(DOMDocument::load("stylesheet.xsl"));  
echo$proc->transformToXML(DOMDocument::load("data.xml"));  

Post a Comment for "Pull Specific Xml Node(s) Into Html Document"