当前位置: 代码迷 >> XML/SOAP >> 关于XSLT中的模板有关问题
  详细解决方案

关于XSLT中的模板有关问题

热度:585   发布时间:2012-02-04 15:43:08.0
关于XSLT中的模板问题
xml文件: books.xml
XML code
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="Books_apply_templates.xsl"?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>





采用的XSL文件:Books_apply_templates.xsl
XML code
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />
  <xsl:template match="/">
    <html>
      <body>
        <h2>My Book Collection</h2>
     <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="book">
    <p>
      <xsl:apply-templates select="title"/>
      <xsl:apply-templates select="price"/>
    </p>
  </xsl:template>
  <xsl:template match="title">
    Title: <span style="color:#ff0000">
      <xsl:value-of select="."/>
    </span>
    <br />
  </xsl:template>
  <xsl:template match="price">
    Artist: <span style="color:#00ff00">
      <xsl:value-of select="."/>
    </span>
    <br />
  </xsl:template>
</xsl:stylesheet>




为什么这样的结果可以正确显示。而当我把代码
XML code
 <body>
        <h2>My Book Collection</h2>
     <xsl:apply-templates />
      </body>

中的
XML code
 <xsl:apply-templates />

更换成
XML code
 <xsl:apply-templates select="book"/>

反而出错。

  我实在是百思不得其解。恳请指教!


------解决方案--------------------
路径不对,改成
XML code
<xsl:apply-templates select="bookstore/book"/>