ホームページ  >  記事  >  バックエンド開発  >  XSLT テンプレートを XML ドキュメントに変換するコード ケース分析の詳細な紹介

XSLT テンプレートを XML ドキュメントに変換するコード ケース分析の詳細な紹介

黄舟
黄舟オリジナル
2017-03-21 16:31:121665ブラウズ

です。通常、このタグには、一致する XML を決定するための match属性 が必要です。ノード。 XML ノード値の選択に使用されるタグは 20ad0a39ace0d7b9b3255414932b514f です。このタグには、どの XML ノードに一致するかを決定するための select 属性が必要です。 。以下では、簡単な例を使用して説明します。次の XML ドキュメントを参照してください:

1 <?xml version="1.0" encoding="utf-8"?>
2 <?xml-stylesheet type="text/xsl" href="stylesheet.xslt"?>
3 <xml>
4   <book>
5     <name>Xml应用系列</name>
6     <author>学路的小孩</author>
7     <date>2009-03-23</date>
8   </book>
9 </xml>


コードの説明: 最初の行は XML ファイルの宣言部分で、2 行目は XSLT ファイルの導入と type 属性を宣言します。 text/xslhref属性は、stylesheet.xsltという名前のXSLTファイルを指します。 3行目以降はXMLドキュメント部分です。以下は stylesheet.xslt の内容です:

1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 3   <xsl:template match="/">
 4     <html>
 5       <head>
 6         <title>第一个XSLT文件</title>
 7       </head>
 8       <body>
 9         <xsl:apply-templates select="xml"></xsl:apply-templates>
10       </body>
11     </html>
12   </xsl:template>
13   <xsl:template match="xml"> 
14     <table style="background-color:orange">
15       <tr>
16         <th>书名</th>
17         <th>作者</th>
18         <th>日期</th>
19       </tr>
20       <xsl:apply-templates select="book"></xsl:apply-templates>
21     </table>
22   </xsl:template>
23   <xsl:template match="book">
24     <tr>
25       <td>
26         <xsl:value-of select="name"/>
27       </td>
28       <td>
29         <xsl:value-of select="author"/>
30       </td>
31       <td>
32         <xsl:value-of select="date"/>
33       </td>
34     </tr>
35   </xsl:template>
36 </xsl:stylesheet>


コードの説明: XSLT ドキュメントの形式はまだ XML であるため 形式なので、 1行目はXMLのヘッダー宣言、2行目はXSLTのバージョンとnamespace宣言、タグはXSLTドキュメントのヒールノードです。 3 行目は、2032d1f3e843089f381a035edc734156e94ab143aef4805f201a19ba8e8a2d4a を使用してテンプレートを作成します。select="/" は、ドキュメントのルート ノードが一致することを意味します。 4 行目から 11 行目は、このノードによって生成される HTML ノード情報です。 9 行目の c2ce39d392085d28c75651ff348a246e タグは、アプリケーション テンプレートを表します。ここで、 select= " xml"は、呼び出されるテンプレートがXMLノードに一致するテンプレートであることを示します。このテンプレートは13行目に表示されます。後続のすべての行 (26 の行などを除く) は、大した説明もなく、これらの内容の繰り返しにすぎません。 26行目はnameタグを選択した内容です。 IEを使用してXMLファイルを開くと、表示される内容は次のとおりです:

 

     另外,XSLT还具有流程控制、条件选择、循环处理、元素排序等功能。下面通过一个实例来说明,其中XML文档内容如下:

1 <?xml version="1.0" encoding="utf-8" ?> 
 2 <?xml-stylesheet type="text/xsl" href="bookListStyle.xslt"?>
 3 <bookList>
 4   <category type="计算机">
 5     <book id="1">
 6       <title>网页与Web程序设计</title>
 7       <author>吴利兵</author>
 8       <pubInfo>
 9         <publisher>机械工业出版社</publisher>
10         <pubDate>2009-04-01</pubDate>
11         <price>16.50</price>
12       </pubInfo>
13     </book>
14     <book id="2">
15       <title>软件工程</title>
16       <author>邓良松</author>
17       <pubInfo>
18         <publisher>西安电子科技出版社</publisher>
19         <pubDate>2005-06-10</pubDate>
20         <price>33.20</price>
21       </pubInfo>
22     </book>
23   </category>
24   <category type="小说">
25     <book id="3">
26       <title>茶花女</title>
27       <author>小仲马</author>
28       <pubInfo>
29         <publisher>外语出版社</publisher>
30         <pubDate>2005-06-30</pubDate>
31         <price>22.00</price>
32       </pubInfo>
33     </book>
34     <book id="4">
35       <title>红楼梦</title>
36       <author>曹雪芹</author>
37       <pubInfo>
38         <publisher>中国教育出版社</publisher>
39         <pubDate>2005-09-06</pubDate>
40         <price>55.00</price>
41       </pubInfo>
42     </book>
43   </category>
44 </bookList>


     bookListStyle.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>
        <style>
          <![CDATA[
            body,td,th{
              font-size:10pt;
              font-family:宋体;
            }
            body{
              background-color:#c0c0c0;
            }
            table{
              border:solid red 1px;
              margin-left:30px;
              margin-right:30px;
              background-color:#ffffc0;
              cellPadding:4;
            }
          ]]>
        </style>
      </head>
      <body>
        <table>
          <caption align="top" style="font-weight:bold; text-align:left">图书列表</caption>
          <tr style="color:#8b0000" align="left">
            <th width="5%">编号</th>
            <th width="10%">类别</th>
            <th width="25%">书名</th>
            <th width="20%">作者</th>
            <th width="25%">出版社</th>
            <th width="10%">出版日期</th>
            <th width="5%">定价</th>
          </tr>
          <xsl:for-each select="bookList/category/book">
            <xsl:sort select="pubInfo/price" order="descending"/>
            <tr>
              <xsl:attribute name="style">
                color:
                <xsl:if test="../@type[.=&#39;计算机&#39;]">blue</xsl:if>
              </xsl:attribute>
              <xsl:attribute name="title">
                <xsl:value-of select="title"/>
                <xsl:choose>
                  <xsl:when test="../@type[.=&#39;计算机&#39;]">
        类别:计算机类图书
                  </xsl:when>
                  <xsl:otherwise>
        类别:小说类图书
                  </xsl:otherwise>
                </xsl:choose>
        作者:<xsl:value-of select="author"></xsl:value-of>
                <br/>
        出版社:<xsl:value-of select="pubInfo/publisher"/>
                <br/>
        出版日期:<xsl:value-of select="pubInfo/pubDate"/>
                <br/>
        定价:<xsl:value-of select="pubInfo/price"/>元
              </xsl:attribute>
              <td>
                <xsl:value-of select="@id"/>
              </td>
              <td>
                <xsl:value-of select="../@type"/>
              </td>
              <td>
                <xsl:value-of select="title"/>
              </td>
              <td>
                <xsl:value-of select="author"/>
              </td>
              <td>
                <xsl:value-of select="pubInfo/publisher"/>
              </td>
              <td>
                <xsl:value-of select="pubInfo/pubDate"/>
              </td>
              <td>
                <xsl:value-of select="pubInfo/price"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

 

     这里不再对代码进行分析,请读者自己理解这段代码,并动手写一下自己的XSLT模板。这段代码的运行效果如下图:

 

 

以上がXSLT テンプレートを XML ドキュメントに変換するコード ケース分析の詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。