【实验目的】
掌握XSL格式化XML输出的一般设计。
【实验环境】
VS.NET
【设计参考】
- 设计XML文件
- 设计XSL文件
- 运行效果
源代码如下:
a.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="b.xslt"?>
<xml>
<TITLE0>五言绝句</TITLE0>
<poetry>
<TITLE1>静夜思</TITLE1>
<author>李白</author>
<content>
<verse>床前明月光,疑是地上霜。</verse>
<verse1>举头望明月,低头思故乡。</verse1>
</content>
</poetry>
<poetry>
<TITLE1>登鹳雀楼</TITLE1>
<author>王之涣</author>
<content>
<verse>白日依山尽,黄河入海流。</verse>
<verse1>欲穷千里目,更上一层楼。</verse1>
</content>
</poetry>
<poetry>
<TITLE1>相思</TITLE1>
<author>王维</author>
<content>
<verse>红豆生南国,春来发几枝。</verse>
<verse1>愿君多采撷,此物最相思。</verse1>
</content>
</poetry>
</xml>
b.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>五言绝句</title>
</head>
<body>
<xsl:apply-templates select="xml"/>
</body>
</html>
</xsl:template>
<xsl:template match="xml">
<div>
<TITLE0><xsl:value-of select="TITLE0"/></TITLE0>
<xsl:for-each select="poetry" >
<poetry>
<TITLE1><xsl:value-of select="TITLE1"/></TITLE1>
<author><xsl:value-of select="author"/></author>
<content>
<xsl:value-of select="content/verse"/><br/>
<xsl:value-of select="content/verse1"/>
</content>
</poetry>
</xsl:for-each>
</div>
<style>
TITLE0 {
font-size: 25px;
}
TITLE1 {
text-decoration: underline;
}
author{
font-style:italic;
}
poetry {
border: 6px solid rgb(10, 10, 10);
margin: 5px;
background-color:grey;
width: 800px;
}
div , poetry , content{
display: flex;
flex-direction: column;
}
TITLE1 , author, content , poetry , TITLE0 {
align-self: center;
}
div {
justify-content: center;
}
</style>
</xsl:template>
</xsl:stylesheet>
运行结果: