Home Backend Development XML/RSS Tutorial Sample code to convert xml to html using xlst

Sample code to convert xml to html using xlst

Mar 20, 2017 pm 05:01 PM

xml文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

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

Copy after login


xslt文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

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

Copy after login


生成html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

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

Copy after login




The above is the detailed content of Sample code to convert xml to html using xlst. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles