Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

html - XHTML to XML XSLT conversion

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.

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>

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.

The source html looks like:

<!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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

XSLT 1.0:

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...