>  기사  >  백엔드 개발  >  XSLT 템플릿을 XML 문서로 변환하는 코드 사례 분석에 대한 자세한 소개

XSLT 템플릿을 XML 문서로 변환하는 코드 사례 분석에 대한 자세한 소개

黄舟
黄舟원래의
2017-03-21 16:31:121666검색

입니다. 일반적으로 이 태그에는 일치 속성 은 일치하는 XML 노드를 결정하는 데 사용됩니다. XML 노드 값을 선택하는 데 사용되는 태그는 20ad0a39ace0d7b9b3255414932b514f입니다. 이 태그에는 선택 속성을 ​​사용하여 일치하는 XML 노드를 결정합니다. 설명을 위해 아래에 간단한 예가 사용됩니다. 다음 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 파일입니다. 선언 부분; 두 번째 줄은 XSLT 파일의 도입을 선언하며 type 속성 설명 파일 형식은 text/ xsl, href 속성은 이름이 XSLT 파일을 가리킵니다. >스타일시트.xslt. 세 번째 줄 다음은 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 형식이므로 첫 번째 줄 XML의 헤더 선언입니다. 두 번째 줄은 XSLT 버전과 네임스페이스 선언입니다. 태그는 XSLT문서의 다음 노드입니다. 세 번째 줄은 2032d1f3e843089f381a035edc734156e94ab143aef4805f201a19ba8e8a2d4a을 사용하여 템플릿을 생성합니다. select="/" 일치 항목이 문서의 루트 노드임을 나타냅니다. 4~11번째 줄은 이 노드에서 생성할 HTML 노드 정보이고, 그 중 9번째 줄은 태그는 애플리케이션 템플릿을 나타내며, 여기서 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.