I am trying to convert my xml document into html web page using apache mod xslt

时间:2016-04-04 18:06:37

标签: xml xslt xhtml

This is my xml document.

<?xml version="1.0" encoding="ISO-8859-15"?>
<?xml-stylesheet type="txt/xsl" "href"="demo1.xsl"?>
<demo>
    <root>
        <_shards>
            <total>1</total>
            <failed>0</failed>
            <successful>1</successful>
        </_shards>
        <hits>
            <hits>
                <highlight>
Hadoop Cloudera Hortonworks Other Search and Big Data Partners Products Technology Overview <em>Aspire</em> Content Representative Customers Case Studies Resources Blog White Papers Videos <em>Aspire</em> Downloads <em>Aspire</em> Wiki Technical Briefs Company Introducing Search Technologies Executive Team Partners Press
                </highlight>
                <_index>newindex3</_index>
                <_type>SearchTech</_type>
                <_id>http://www.searchtechnologies.com/support</_id>
                <_score>0.1789403</_score>
                <fields>
                    <keywords>keywords-NOT-PROVIDED</keywords>
                    <title>Search Technologies</title>
                    <url>http://www.searchtechnologies.com/support</url>
                </fields>
            </hits>
            <hits>
                <highlight>
Hadoop Cloudera Hortonworks Other Search and Big Data Partners Products Technology Overview <em>Aspire</em> Content Representative Customers Case Studies Resources Blog White Papers Videos <em>Aspire</em> Downloads <em>Aspire</em> Wiki Technical Briefs Company Introducing Search Technologies Executive Team Partners Press
                </highlight>
                <_index>newindex3</_index>
                <_type>SearchTech</_type>
                <_id>http://www.searchtechnologies.com/</_id>
                <_score>0.1491169</_score>
                <fields>
                    <keywords>
Enterprise Search, Big Data, Analytics, Consulting, Search Engine Experts, Big Data Services
                    </keywords>
                    <title>Enterprise Search and Big Data Experts</title>
                    <url>http://www.searchtechnologies.com/</url>
                </fields>
            </hits>
            <total>2</total>
            <max_score>0.1789403</max_score>
        </hits>
        <took>3</took>
        <timed_out>false</timed_out>
    </root>
</demo>

This is my xslt code. I am trying to filter only the highlight field from my xml. The match attribute is used to associate a template with an XML element.

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

        <xsl:template match="/">
          <html>
          <body>
          <h2>My DEMO</h2>
          <table border="1">
            <tr bgcolor="#9acd32">
              <th>highlight</th>
            </tr>
            <xsl:for-each select="demo/root/hits/hits">
            <tr>
              <td><xsl:value-of select="highlight"/></td>
            </tr>
            </xsl:for-each>
          </table>
          </body>
          </html>
        </xsl:template>

        </xsl:stylesheet>

When I go to my server: http://yourserveraddress/xml/filename.xml I am getting an error that "This XML file does not appear to have any style information associated with it"

I make correction and converted "txt" into "text", now when I go to my server I got the following output instead of an html web page.

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<?xml-stylesheet type="text/xsl" "href"="demo1.xsl"?>
<demo>
<root>
<_shards>
<total>1</total>
<failed>0</failed>
<successful>1</successful>
</_shards>
<hits>
<hits>
<highlight>
Hadoop Cloudera Hortonworks Other Search and Big Data Partners Products Technology Overview
<em>Aspire</em>
Content Representative Customers Case Studies Resources Blog White Papers Videos
<em>Aspire</em>
Downloads
<em>Aspire</em>
Wiki Technical Briefs Company Introducing Search Technologies Executive Team Partners Press
</highlight>
<_index>newindex3</_index>
<_type>SearchTech</_type>
<_id>http://www.searchtechnologies.com/support</_id>
<_score>0.1789403</_score>
<fields>
<keywords>keywords-NOT-PROVIDED</keywords>
<title>Search Technologies</title>
<url>http://www.searchtechnologies.com/support</url>
</fields>
</hits>
<hits>
<highlight>
Hadoop Cloudera Hortonworks Other Search and Big Data Partners Products Technology Overview
<em>Aspire</em>
Content Representative Customers Case Studies Resources Blog White Papers Videos
<em>Aspire</em>
Downloads
<em>Aspire</em>
Wiki Technical Briefs Company Introducing Search Technologies Executive Team Partners Press
</highlight>
<_index>newindex3</_index>
<_type>SearchTech</_type>
<_id>http://www.searchtechnologies.com/</_id>
<_score>0.1491169</_score>
<fields>
<keywords>
Enterprise Search, Big Data, Analytics, Consulting, Search Engine Experts, Big Data Services
</keywords>
<title>Enterprise Search and Big Dataà Experts</title>
<url>http://www.searchtechnologies.com/</url>
</fields>
</hits>
<total>2</total>
<max_score>0.1789403</max_score>
</hits>
<took>3</took>
<timed_out>false</timed_out>
</root>
</demo>

1 个答案:

答案 0 :(得分:0)

Your input with Hadoop Cloudera Hortonworks Other Search & Big ... is not well-formed XML as the ampersand would need to be escaped as Hadoop Cloudera Hortonworks Other Search &amp; Big.... So you need to fix the input to be well-formed before being able to process it with XML tools like XSLT.

Also correct <?xml-stylesheet type="txt/xsl" "href"="demo1.xsl"?> to <?xml-stylesheet type="text/xsl" href="demo1.xsl"?>.