XHTML 到 XML XSLT 的转换 [英] XHTML to XML XSLT conversion

查看:40
本文介绍了XHTML 到 XML XSLT 的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个非常简单的 xslt 将 html 页面转换为 xml 文件.

I am doing a very simple xslt to convert a html page to a xml file.

但在我看来,起点对我来说并不是那么简单.我的第一个目标是将 标签转换为 > 标签.

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>

专家能帮我一点忙吗?

推荐答案

XSLT 1.0:

尝试将 xmlns:x="http://www.w3.org/1999/xhtml" 添加到您的 xsl:stylesheet 并将您的匹配更改为 match="x: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆