I have a simple XML file:
我有一个简单的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Page x1="71" y1="120" x2="527" y2="765" type="page" chunkCount="1" pageNumber="1" wordCount="1">...</Page>
<Chunk x1="206" y1="120" x2="388" y2="144" type="unclassified">
<Word x1="206" y1="120" x2="214" y2="144" font="Times-Roman" style="font-size:22pt">WORD</Word>
</Chunk>
</Document>
When trying to view it, my browser tell's me I am missing a stylesheet. As I have no previous experience with XML, my question is: how would a simple XSLT (as I understand?) look like, that would enable me to view each element of the XML file in a position given by coordinates in the file. Help greatly appriciated.
当我试图查看它时,我的浏览器告诉我,我错过了样式表。由于我之前没有使用过XML的经验,我的问题是:一个简单的XSLT(我理解?)是什么样的,这样我就可以在文件坐标给出的位置查看XML文件的每个元素。帮助大大苛求。
2 个解决方案
#1
2
Your XML above has errors, you close Page twice, and Chunk is incorrectly closed with Chuck
上面的XML有错误,你关闭Page两次,Chunk被Chuck错误关闭
having a xslt (style sheet) specified on the xml file is optional, the browser is just infoming why he shows raw xml, thats ok. you can apply xsl styles on server side and decide what sheet to apply, no need to refer it on the xml file. also you can do xslt translation on client side, this will be more tricky because browser differ the implementation to achieve that.
在xml文件上指定xslt(样式表)是可选的,浏览器只是为什么他显示原始xml,这没关系。您可以在服务器端应用xsl样式并确定要应用的表单,无需在xml文件中引用它。你也可以在客户端进行xslt转换,这将更加棘手,因为浏览器实现了不同的实现。
however if you specify a style sheet all major browsers will translate on client side (old browser don't) but even IE6 does
但是,如果您指定样式表,所有主要浏览器都将在客户端进行翻译(旧浏览器不会),但即使是IE6也可以
a link to a style sheet specified on xml file would look like:
指向xml文件上指定的样式表的链接如下所示:
<?xml-stylesheet type="text/xsl" href="so.xslt"?>
a style sheet can look like:
样式表可以如下所示:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output omit-xml-declaration="yes" method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Document">
<html>
<head><title>Test</title></head>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="Page">
<body>
<xsl:apply-templates/><hr/>
Page:<xsl:value-of select="@pageNumber"/>
</body>
</xsl:template>
<xsl:template match="Chunk">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
but as you can see you can build almost anything with that.
但正如你所看到的,你几乎可以用它构建任何东西。
#2
-1
Just add xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" to your html tag. That worked for me.
只需将xmlns =“http://www.w3.org/1999/xhtml”xml:lang =“en”lang =“en”添加到您的html标记中。这对我有用。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
</head>
<body>
</body>
</html>
#1
2
Your XML above has errors, you close Page twice, and Chunk is incorrectly closed with Chuck
上面的XML有错误,你关闭Page两次,Chunk被Chuck错误关闭
having a xslt (style sheet) specified on the xml file is optional, the browser is just infoming why he shows raw xml, thats ok. you can apply xsl styles on server side and decide what sheet to apply, no need to refer it on the xml file. also you can do xslt translation on client side, this will be more tricky because browser differ the implementation to achieve that.
在xml文件上指定xslt(样式表)是可选的,浏览器只是为什么他显示原始xml,这没关系。您可以在服务器端应用xsl样式并确定要应用的表单,无需在xml文件中引用它。你也可以在客户端进行xslt转换,这将更加棘手,因为浏览器实现了不同的实现。
however if you specify a style sheet all major browsers will translate on client side (old browser don't) but even IE6 does
但是,如果您指定样式表,所有主要浏览器都将在客户端进行翻译(旧浏览器不会),但即使是IE6也可以
a link to a style sheet specified on xml file would look like:
指向xml文件上指定的样式表的链接如下所示:
<?xml-stylesheet type="text/xsl" href="so.xslt"?>
a style sheet can look like:
样式表可以如下所示:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output omit-xml-declaration="yes" method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Document">
<html>
<head><title>Test</title></head>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="Page">
<body>
<xsl:apply-templates/><hr/>
Page:<xsl:value-of select="@pageNumber"/>
</body>
</xsl:template>
<xsl:template match="Chunk">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
but as you can see you can build almost anything with that.
但正如你所看到的,你几乎可以用它构建任何东西。
#2
-1
Just add xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" to your html tag. That worked for me.
只需将xmlns =“http://www.w3.org/1999/xhtml”xml:lang =“en”lang =“en”添加到您的html标记中。这对我有用。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
</head>
<body>
</body>
</html>