简体   繁体   English

使用XSLT将XML转换为HTML

[英]XML to HTML transformation using XSLT

I need some help converting an XML file to HTML using XSLT. 我需要一些使用XSLT将XML文件转换为HTML的帮助。 The problem I'm having is that the for-each loop is skipped. 我遇到的问题是for-each循环被跳过。 The parser can't find any "file" elements under "x:imagelist". 解析器在“ x:imagelist”下找不到任何“文件”元素。 What am I doing wrong? 我究竟做错了什么? I already tried suggestions from other posts without any luck. 我已经尝试了其他帖子的建议,但没有任何运气。

Here is the XML source file: 这是XML源文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<x:imagelist xmlns:x="ext:extractor">
    <file>
        <fileName>2012-04-28 16</fileName>
        <format>jpg</format>
        <width>3264</width>
        <height>1840</height>
        <dateTime>Sat Apr 28 16:49:22 CEST 2012</dateTime>
        <cameraModel>HTC, HTC One X</cameraModel>
        <ISO>100</ISO>
        <exposure>8.0E-4</exposure>
        <fStop>F2</fStop>
    </file>
    <file>
        <fileName>2012-04-28 16</fileName>
        <format>jpg</format>
        <width>3264</width>
        <height>1840</height>
        <dateTime>Sat Apr 28 16:49:24 CEST 2012</dateTime>
        <cameraModel>HTC, HTC One X</cameraModel>
        <ISO>100</ISO>
        <exposure>8.0E-4</exposure>
        <fStop>F2</fStop>
        </file>
    <file>
        <fileName>2012-04-28 16</fileName>
        <format>jpg</format>
        <width>3264</width>
        <height>1840</height>
        <dateTime>Sat Apr 28 16:49:32 CEST 2012</dateTime>
        <cameraModel>HTC, HTC One X</cameraModel>
        <ISO>100</ISO>
        <exposure>6.0E-4</exposure>
        <fStop>F2</fStop>
        </file>
    <location>C:\Users\Serban\Documents\pics</location>
</x:imagelist>

And here is the XSL file: 这是XSL文件:

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

<html>
<head>
    <title>My Images</title>
    <link rel="stylesheet" type="text/css" href="style_light.css"></link>
</head>

<body>

<div class="everything">
    <div class="headerDiv">
        <h1>MY IMAGES</h1>
        <h3>View your image list online!</h3>
    </div>

    <div class="contentDiv">
    <table class="dataTable">
        <tr class="tableHeader">
            <th>File Name</th><th>Format</th><th>Width</th><th>Height</th><th>Date and Time</th><th>Camera Model</th><th>ISO</th><th>Exposure</th><th>F-Stop</th>
        </tr>

        <xsl:for-each select="x:imagelist/file">
        <tr>
        <td><xsl:value-of select="fileName"></xsl:value-of></td>
        <td><xsl:value-of select="format"></xsl:value-of></td>
        <td><xsl:value-of select="width"></xsl:value-of></td>
        <td><xsl:value-of select="height"></xsl:value-of></td>
        <td><xsl:value-of select="dateTime"></xsl:value-of></td>
        <td><xsl:value-of select="cameraModel"></xsl:value-of></td>
        <td><xsl:value-of select="ISO"></xsl:value-of></td>
        <td><xsl:value-of select="exposure"></xsl:value-of></td>
        <td><xsl:value-of select="fStop"></xsl:value-of></td>
        </tr>
        </xsl:for-each>

    </table>
    </div>
</div>
</body>
</html> 

</xsl:template>
</xsl:stylesheet>

You have a namespace mismatch between your source XML and your XSL transformation. 您的源XML和XSL转换之间的名称空间不匹配。

In the XML, namespace prefix x is bound to ext:extractor . 在XML中,名称空间前缀x绑定到ext:extractor In the XSL, it's bound to http://something//x . 在XSL中,它绑定到http://something//x Change the stylesheet to match the XML, and your transformation works as intended. 更改样式表以匹配XML,您的转换将按预期工作。

Example: http://xsltransform.net/948Fn5d 示例: http//xsltransform.net/948Fn5d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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