XHTML转换为XML XSLT [英] XHTML to XML XSLT conversion
问题描述
我正在做一个非常简单的xslt来将html页面转换为xml文件。
I am doing a very simple xslt to convert a html page to a xml file.
但在我看来,起点对我来说并不那么直截了当。我的第一个目标是将< html>
标记转换为< topic>
标记。
But it appears to me that the starting point is not that straightforward to me.My first goal is to convert a <html>
tag into a <topic>
tag.
我做了以下xslt:
I did the following xslt:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="html">
<topic>
<xsl:text> Conversion Test</xsl:text>
</topic>
</xsl:template>
但是,现在我运行这个xslt后,结果xml完全与原始的内容相同html页面,似乎我写的第三个模板匹配(匹配< html>
标签)永远不会被打。
However, now after I run this xslt, the result xml is purely of the same content of the original html page, it seems that the third template match that I wrote (to match the <html>
tag) never gets hit.
源html类似于:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>..</head>
<body>...</body>
</html>
请高手帮我一下吗?
Could experts help me a little here?
推荐答案
XSLT 1.0:
尝试添加 xmlns:x = http://www.w3.org/1999/xhtml
添加到 xsl:stylesheet
并将您的匹配更改为匹配= ×:HTML
。 (注意:您不必使用x;您可以选择任何您想要的内容。)
Try adding xmlns:x="http://www.w3.org/1999/xhtml"
to your xsl:stylesheet
and changing your match to match="x:html"
. (Note: you don't have to use "x"; you can choose anything you want.)
XSLT 2.0:
使用上述方法或将匹配中的命名空间前缀替换为*( match =*:html
)。您还可以将 xpath-default-namespace =http://www.w3.org/1999/xhtml
添加到 xsl:stylesheet
。
Either use the above method or replace the namespace prefix in your match(es) to "*" (match="*:html"
). You could also add xpath-default-namespace="http://www.w3.org/1999/xhtml"
to the xsl:stylesheet
.
这篇关于XHTML转换为XML XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!