백엔드 개발 XML/RSS 튜토리얼 xlst를 사용하여 xml을 html로 변환하는 샘플 코드

xlst를 사용하여 xml을 html로 변환하는 샘플 코드

Mar 20, 2017 pm 05:01 PM

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으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

HTML의 테이블 테두리 HTML의 테이블 테두리 Sep 04, 2024 pm 04:49 PM

HTML의 테이블 테두리 안내. 여기에서는 HTML의 테이블 테두리 예제를 사용하여 테이블 테두리를 정의하는 여러 가지 방법을 논의합니다.

HTML의 중첩 테이블 HTML의 중첩 테이블 Sep 04, 2024 pm 04:49 PM

HTML의 Nested Table에 대한 안내입니다. 여기에서는 각 예와 함께 테이블 내에 테이블을 만드는 방법을 설명합니다.

HTML 여백-왼쪽 HTML 여백-왼쪽 Sep 04, 2024 pm 04:48 PM

HTML 여백-왼쪽 안내. 여기에서는 HTML margin-left에 대한 간략한 개요와 코드 구현과 함께 예제를 논의합니다.

HTML 테이블 레이아웃 HTML 테이블 레이아웃 Sep 04, 2024 pm 04:54 PM

HTML 테이블 레이아웃 안내. 여기에서는 HTML 테이블 레이아웃의 값에 대해 예제 및 출력 n 세부 사항과 함께 논의합니다.

HTML 정렬 목록 HTML 정렬 목록 Sep 04, 2024 pm 04:43 PM

HTML 순서 목록에 대한 안내입니다. 여기서는 HTML Ordered 목록 및 유형에 대한 소개와 각각의 예에 대해서도 설명합니다.

HTML에서 텍스트 이동 HTML에서 텍스트 이동 Sep 04, 2024 pm 04:45 PM

HTML에서 텍스트 이동 안내. 여기서는 Marquee 태그가 구문과 함께 작동하는 방식과 구현할 예제에 대해 소개합니다.

HTML 입력 자리 표시자 HTML 입력 자리 표시자 Sep 04, 2024 pm 04:54 PM

HTML 입력 자리 표시자 안내. 여기서는 코드 및 출력과 함께 HTML 입력 자리 표시자의 예를 논의합니다.

PHP에서 HTML/XML을 어떻게 구문 분석하고 처리합니까? PHP에서 HTML/XML을 어떻게 구문 분석하고 처리합니까? Feb 07, 2025 am 11:57 AM

이 튜토리얼은 PHP를 사용하여 XML 문서를 효율적으로 처리하는 방법을 보여줍니다. XML (Extensible Markup Language)은 인간의 가독성과 기계 구문 분석을 위해 설계된 다목적 텍스트 기반 마크 업 언어입니다. 일반적으로 데이터 저장 AN에 사용됩니다

See all articles