Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting HTML to XML using XSLT

Tags:

xml

xhtml

xslt

I'm trying to convert an XHTML document to XML using XSLT but I'm currently having trouble getting my templates to match the tags in the input document. Should I be able to convert XHTML to XML like this? If so is there an error in my stylesheet?

Input Document:

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>      
        <title>title text</title>       
    </head>
    <body>      
        <p>body text</p>
    </body>
</html>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes" />


    <xsl:template match="/"> 
    <article>       
        <xsl:apply-templates select="html/head"></xsl:apply-templates>
    </article>
    </xsl:template>



    <xsl:template match="html/head">
        <head><xsl:text>This is where all the metadata will come from</xsl:text></head>
    </xsl:template> 
</xsl:stylesheet>

Expected Output

<article>       
  <head>This is where all the metadata will come from</head>        
</article>

Thanks

like image 708
JimS Avatar asked May 23 '13 10:05

JimS


People also ask

How XML is converted into HTML format using XSLT?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

How create XML from XSLT?

Execute an XSLT transformation from an XML fileOpen an XML document in the XML editor. Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document. For example, add the following line to the document prolog: <?

Can I use XSL in HTML?

XSL can be used server-side and client-side. The XSL Submission has two classes of output: DSSSL-style flow objects and HTML tags.

What is XSLT in HTML?

XSL (eXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations. This tutorial will teach you how to use XSLT to transform XML documents into other formats (like transforming XML into HTML).


1 Answers

The elements within your XHTML document are in the http://www.w3.org/1999/xhtml namespace. Whereas your XSLT document is matching elements that do not have a namespace. You need to add a namespace as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xhtml="http://www.idpf.org/2007/opf">

    ...
    <xsl:template match="xhtml:html/xhtml:head">
        <head><xsl:text>This is where all the metadata will come from</xsl:text></head>
    </xsl:template> 
</xsl:stylesheet>
like image 108
ColinE Avatar answered Sep 28 '22 17:09

ColinE