簡體   English   中英

使用XSLT將XML轉換為HTML

[英]XML to HTML transformation using XSLT

我需要一些使用XSLT將XML文件轉換為HTML的幫助。 我遇到的問題是for-each循環被跳過。 解析器在“ x:imagelist”下找不到任何“文件”元素。 我究竟做錯了什么? 我已經嘗試了其他帖子的建議,但沒有任何運氣。

這是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>

這是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>

您的源XML和XSL轉換之間的名稱空間不匹配。

在XML中,名稱空間前綴x綁定到ext:extractor 在XSL中,它綁定到http://something//x 更改樣式表以匹配XML,您的轉換將按預期工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM