您好,登錄后才能下訂單哦!
這篇文章主要介紹如何使用xlst將xml轉(zhuǎn)換html,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
xml文件
<?xml version="1.0" encoding="utf-8" ?> <Paper Title="小寒考試系統(tǒng)" Name="大三歷史期末考試" Start Time ="2008-1-28 09:00" Time="120" ScoreValue="100" Score="0"> <Student Name="小寒" Id="041124096"/> <Questions Title="單選題" ScoreValue="20" Count ="1" Score="0"> <DanXuan Id="1" Subject="歷史" Chapter="第三章" D if ficulty="1" ScoreValue="20" Score="0"> <Content>諸葛亮姓什么?</Content> <Choices> <Choice Key ="1">諸</Choice> <Choice Key="2">諸葛</Choice> <Choice Key="3">諸葛亮</Choice> <Choice Key="4">亮</Choice> </Choices> <Answer>2</Answer> <StudentAnswer></StudentAnswer> </DanXuan> </Questions> <Questions Title="多選題" ScoreValue="20" Count="1" Score="0"> <DuoXuan Id="2" Subject="歷史" Chapter="第三章" Difficulty="1" ScoreValue="20" Score="0"> <Content>三國是指那三國?</Content> <Choices> <Choice Key="1">魏國</Choice> <Choice Key="2">吳國</Choice> <Choice Key="3">遼國</Choice> <Choice Key="4">蜀國</Choice> </Choices> <Answer>1,2,4</Answer> <StudentAnswer></StudentAnswer> </DuoXuan> </Questions> <Questions Title="判斷題" ScoreValue="20" Count="1" Score="0"> <PanDuan Id="3" Subject="歷史" Chapter="第三章" Difficulty="1" ScoreValue="20" Score="0"> <Content>劉備建立了蜀國?</Content> <Choices> <Choice Key="0">0</Choice> <Choice Key="1">1</Choice> </Choices> <Answer>1</Answer> <StudentAnswer></StudentAnswer> </PanDuan> </Questions> <Questions Title="填空題" ScoreValue="20" Count="1" Score="0"> <TianKong Id="4" Subject="歷史" Chapter="第三章" Difficulty="1" ScoreValue="20" Score="0"> <Content> <![CDATA[ 三國里的五虎上將是指關(guān)羽,$_4.1_$,$_4.2_$,$_4.3_$,趙云。 ]]> </Content> <Answers> <Answer Key="1">張飛</Answer> <Answer Key="2">魏延</Answer> <Answer Key="3">馬超</Answer> </Answers> <StudentAnswers> <StudentAnswer Key="1"></StudentAnswer> <StudentAnswer Key="2"></StudentAnswer> <StudentAnswer Key="3"></StudentAnswer> </StudentAnswers> </TianKong> </Questions> <Questions Title=" 簡答題 " ScoreValue="20" Count="1" Score="0"> <JianDa Id="5" Subject="歷史" Chapter="第三章" Difficulty="1" ScoreValue="20" Score="0"> <Content>為什么諸葛亮沒能統(tǒng)一三國?</Content> <Answer>因為魏國統(tǒng)一了三國。</Answer> <StudentAnswer></StudentAnswer> </JianDa> </Questions> </Paper>
xslt文件
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Trans for m" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:rules="http://www.netguy.cn/xslt" exclude-result-prefixes="rules msxsl"> <xsl:output method="html"/> <msxsl:script implements-prefix="rules" lang uage=" C# "> <![CDATA[ public static string ChangeTextBox(string content) { // Define a regular expression for repeated w ord s. Regex rx = new Regex(@"\$_\S\S\S_\$", RegexOptions.Compiled | RegexOptions.IgnoreCase); // Find matches. MatchCollection matches = rx.Matches(content); // Report on each match. foreach (Match match in matches) { string word = match.Value; content=content.Replace(word,"<input name=\""+word. Substr ing(2,word.Length-4)+"\" type=\"text\">"); } return content; } ]]> </msxsl:script> <xsl:template match="Paper"> <html xmlns="http://www.w3.org/1999/xhtml" > < head > <title> <xsl:value-of select="@Title"/> </title> </head> <body> <p class="Head"> <p class="Name"><xsl:value-of select="@Name"/></p> <p class="Info"> 姓名:<xsl:value-of select="Student/@Name"/> 學(xué)號:<xsl:value-of select="Student/@Id"/> 開考時間:<xsl:value-of select="@StartTime"/> 時間:<xsl:value-of select="@Time"/> 總分:<xsl:value-of select="@ScoreValue"/> </p> </p> <xsl:apply-templates select="Questions"/> </body> </html> </xsl:template> <xsl:template match="Questions"> <p class="Title"> <xsl:number format="I."/> <xsl:value-of select="@Title"/> <xsl:text>。(共</xsl:text> <xsl:value-of select="@Count"/> <xsl:text>題,</xsl:text> <xsl:value-of select="@ScoreValue"/> <xsl:text>分)</xsl:text> </p> <xsl:apply-templates select="DanXuan"/> <xsl:apply-templates select="DuoXuan"/> <xsl:apply-templates select="PanDuan"/> <xsl:apply-templates select="TianKong"/> <xsl:apply-templates select="JianDa"/> </xsl:template> <xsl:template match="DanXuan"> <p class="DanXuan"> <p class="Content"> <xsl:number format="1."/> <xsl:value-of select="Content"/> </p> <p class="Choices"> <ul> <xsl:for-each select="Choices/Choice"> <li> <xsl:number format="A."/> <input name="{http://www.cnblogs.com/@Id}" type="radio" value="{@Key}"/> <xsl:value-of select="."/> </li> </xsl:for-each> </ul> </p> </p> </xsl:template> <xsl:template match="DuoXuan"> <p class="DuoXuan"> <p class="Content"> <xsl:number format="1."/> <xsl:value-of select="Content"/> </p> <p class="Choices"> <ul> <xsl:for-each select="Choices/Choice"> <li> <xsl:number format="A."/> <input name="{http://www.cnblogs.com/@Id}" type="checkbox" value="{@Key}"/> <xsl:value-of select="."/> </li> </xsl:for-each> </ul> </p> </p> </xsl:template> <xsl:template match="PanDuan"> <p class="PanDuan" style=" width : 300 px"> <p class="Content" style=" float :left ;width:70%"> <xsl:number format="1."/> <xsl:value-of select="Content"/> </p> <p class="Choices" style="float :right ;width:25%"> <input name="{@Id}" type="radio" value="1"/>Y <input name="{@Id}" type="radio" value="0" />N </p> </p> </xsl:template> <xsl:template match="TianKong"> <p class="TianKong"> <xsl:number format="1."/> <xsl:value-of select="rules:ChangeTextBox(string(Content))" disable-output-esc api ng="yes"/> </p> </xsl:template> <xsl:template match="JianDa"> <p class="JianDa"> <p class="Content"> <xsl:number format="1."/> <xsl:value-of select="Content"/> </p> <p class="Input"> <textarea name="{@Id}" cols="70" rows="8"></textarea> </p> </p> </xsl:template> </xsl:stylesheet>
生成html
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>小寒考試系統(tǒng)</title> </head> <body> <div class="Head"> <div class="Name">大三歷史期末考試</div> <div class="Info"> 姓名:小寒 學(xué)號:041124096 開考時間:2008-1-28 09:00 時間:120 總分:100</div> </div> <div class="Title" xmlns="">I.單選題。(共1題,20分)</div> <div class="DanXuan" xmlns=""> <div class="Content">1.諸葛亮姓什么?</div> <div class="Choices"> <ul> <li>A.<input name="1" type="radio" value="1">諸</li> <li>B.<input name="1" type="radio" value="2">諸葛</li> <li>C.<input name="1" type="radio" value="3">諸葛亮</li> <li>D.<input name="1" type="radio" value="4">亮</li> </ul> </div> </div> <div class="Title" xmlns="">II.多選題。(共1題,20分)</div> <div class="DuoXuan" xmlns=""> <div class="Content">1.三國是指那三國?</div> <div class="Choices"> <ul> <li>A.<input name="2" type="checkbox" value="1">魏國</li> <li>B.<input name="2" type="checkbox" value="2">吳國</li> <li>C.<input name="2" type="checkbox" value="3">遼國</li> <li>D.<input name="2" type="checkbox" value="4">蜀國</li> </ul> </div> </div> <div class="Title" xmlns="">III.判斷題。(共1題,20分)</div> <div class="PanDuan" style="width:300px" xmlns=""> <div class="Content" style="float:left;width:70%">1.劉備建立了蜀國?</div> <div class="Choices" style="float:right;width:25%"><input name="3" type="radio" value="1">Y <input name="3" type="radio" value="0">N </div> </div> <div class="Title" xmlns="">IV.填空題。(共1題,20分)</div> <div class="TianKong" xmlns="">1. 三國里的五虎上將是指關(guān)羽,<input name="4.1" type="text">,<input name="4.2" type="text">,<input name="4.3" type="text">,趙云。 </div> <div class="Title" xmlns="">V.簡答題。(共1題,20分)</div> <div class="JianDa" xmlns=""> <div class="Content">1.為什么諸葛亮沒能統(tǒng)一三國?</div> <div class="Input"><textarea name="5" cols="70" rows="8"></textarea></div> </div> </body> </html>
以上是“如何使用xlst將xml轉(zhuǎn)換html”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。