搜尋
首頁後端開發XML/RSS教程使用xlst將xml轉換html的範例程式碼

xml文件

<?xml version="1.0" encoding="utf-8" ?>
<Paper Title="小寒考试系统" 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[
      三国里的五虎上将是指关羽,$_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>为什么诸葛亮没能统一三国?</Content>
      <Answer>因为魏国统一了三国。</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"/>
            学号:<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>小寒考试系统</title>
  </head>
  <body>
    <div class="Head">
      <div class="Name">大三历史期末考试</div>
      <div class="Info">
            姓名:小寒
            学号: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. 
      三国里的五虎上将是指关羽,<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.为什么诸葛亮没能统一三国?</div>
      <div class="Input"><textarea name="5" cols="70" rows="8"></textarea></div>
    </div>
  </body>
</html>




以上是使用xlst將xml轉換html的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
掌握良好的XML:數據交換的最佳實踐掌握良好的XML:數據交換的最佳實踐May 14, 2025 am 12:05 AM

良好形式的XMliscrucialfordAtaExchangeBecapeparsingSiturscrectparsingandSundStandingAcrossystem.1)startwithAdeClarationLike.2)確保everyveryopentingtaghasaclopingtaghasaclosingtagosingtagandelementsareproproproproproperlynested.3)

XML:它仍然使用嗎?XML:它仍然使用嗎?May 13, 2025 pm 03:13 PM

Xmlisstillusedduetoitsstructusednature,人類可讀性,以及範圍的addionindererpriseEnvormentments.1)itfacilitatesdataexchangeInsectInsectorlikeFinance(swift)andHealthCare(hl7)和hl7)

RSS文檔的解剖結構:結構和元素RSS文檔的解剖結構:結構和元素May 10, 2025 am 12:23 AM

RSS文檔的結構包括三個主要元素:1.:根元素,定義RSS版本;2.:包含頻道信息,如標題、鏈接、描述;3.:代表具體的內容條目,包含標題、鏈接、描述等。

了解RSS文檔:綜合指南了解RSS文檔:綜合指南May 09, 2025 am 12:15 AM

RSS文檔是一種簡便的訂閱機制,通過XML文件發佈內容更新。 1.RSS文檔結構由和元素組成,包含多個。 2.使用RSS閱讀器訂閱頻道,並通過解析XML提取信息。 3.高級用法包括使用feedparser庫進行過濾和排序。 4.常見錯誤包括XML解析和編碼問題,調試時需驗證XML格式和編碼。 5.性能優化建議包括緩存RSS文檔和異步解析。

RSS,XML和現代網絡:內容聯合深度潛水RSS,XML和現代網絡:內容聯合深度潛水May 08, 2025 am 12:14 AM

RSS和XML在現代Web中依然重要。 1.RSS用於發布和分發內容,用戶可通過RSS閱讀器訂閱並獲取更新。 2.XML作為標記語言,支持數據存儲和交換,RSS文件基於XML。

基礎知識超越:XML啟用的高級RSS功能基礎知識超越:XML啟用的高級RSS功能May 07, 2025 am 12:12 AM

RSS可以實現多媒體內容嵌入、條件訂閱、以及性能和安全性優化。 1)通過標籤嵌入多媒體內容,如音頻和視頻。 2)使用XML命名空間實現條件訂閱,允許訂閱者根據特定條件篩選內容。 3)通過CDATA節和XMLSchema優化RSSFeed的性能和安全性,確保穩定性和符合標準。

解碼RSS:Web開發人員的XML底漆解碼RSS:Web開發人員的XML底漆May 06, 2025 am 12:05 AM

RSS是一種基於XML的格式,用於發布常更新的數據。作為Web開發者,理解RSS能提升內容聚合和自動化更新能力。通過學習RSS結構、解析和生成方法,你將能自信地處理RSSfeeds,優化Web開發技能。

JSON與XML:為什麼RSS選擇XMLJSON與XML:為什麼RSS選擇XMLMay 05, 2025 am 12:01 AM

RSS選擇XML而不是JSON是因為:1)XML的結構化和驗證能力優於JSON,適合RSS複雜數據結構的需求;2)XML當時有廣泛的工具支持;3)RSS早期版本基於XML,已成標準。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具