创建XSLT的问题

时间:2013-12-13 17:55:30

标签: html xslt

给出以下输入和输出XML文档的示例,编写一个XSLT 将在它们之间转换的程序。输出是一个HTML文档。 !

示例输入XML文档:!

  <library name="Library Name 1" address="Address">!
   <libraryItems>!
  <libraryItem type="book" id="123" isbn="123" title="Title1" summary="Summary1"!
   publisher="Publisher1" published="1/2/12" barcode="Code1" 
           rfid="RFID1" added="1/2/12">!
        <author name="Author Name1"/>!
         </libraryItem>!
     ! ...any number of items of type book...!
       <libraryItem type="cd" id="345" title="Title3" tracks="Tracks1" genre="Genre1"!
     ! barcode="Code3" rfid="RFID3" added="1/2/12"/>!
     ! ...any number of items of type cd...!
    </libraryItems>!
  <accounts>!
   <account number="234" state="ACTIVE">!
       <patron firstName="Firstname1" lastName="Lastname1" address="Address1"/>!
    <borrows item="123"/>!
     ! ...any number of borrows...!
      </account>!
      ! ...any number of accounts...!
     </accounts>!
       </library>! !

示例输出HTML文档:!

<h1>Borrowed Items</h1>!
<table><tr>! <th>Library</th><th>Patron</th><th>Borrowing</th>! </tr>!
! <tr>! <td>Library Name 1</td><td>Firstname1 Lastname1</td><td>Title1</td></tr>!
! <tr>! <td>Library Name 1</td><td>Firstname2 Lastname2</td><td>Title2</td></tr>!
! ...one row for every borrowed item...!
</table>

这是给出三个错误的解决方案:

      <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
 <h1>Borrowed Items</h1>!
<table>
<tr>! 
<th>Library</th>
<th>Patron</th>
<th>Borrowing</th>!
 </tr>!

<xsl:for-each select="//library/libraryitems/libraryitem | //library/accounts/account">

 <tr>
<td>library name 1</td>      
  <td><xsl:value-of select="firstName"/><xsl:value-of select="lastName"/></td>
  <td><xsl:value-of select="title"/></td>    </tr>
 </xsl:for-each>
</table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

我应该做些什么更正?

2 个答案:

答案 0 :(得分:0)

该解决方案的一个问题是如何访问XML标记属性,XSL使用XPath来导航XML结构,这与文件系统导航语法非常相似,假设您在目录中并且你想上路或下路。当你有一个属性时,你需要放置@attributeName,所以名字实际上是@firstName,但你需要在正确的标签中。

此外,当您必须将借用的ID与标题ID匹配时,您只需要使用“..”导航回来,然后导航到您想要的libraryItem,对于匹配我觉得最好使用'key'标签虽然。

看看下面的解决方案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="title" match="libraryItem" use="@id"/>
<xsl:template match="/">
    <html>
    <body>
    <h1>Borrowed Items</h1>
    <table border="1">
    <tr bgcolor="#9acd32">
        <th>Library</th>
        <th>Patron</th>
        <th>Borrowing</th>
    </tr>
        <xsl:for-each select="library/accounts/account">
            <xsl:for-each select="borrows">
                <tr>
                    <td><xsl:value-of select="../../../@name"/></td>
                    <td><xsl:value-of  select="../patron/@firstName"/><xsl:text> </xsl:text><xsl:value-of select="../patron/@lastName"/></td>
                    <td><xsl:value-of select="key('title', @item)/@title"/></td>
                </tr>
            </xsl:for-each> 
        </xsl:for-each>    
    </table>
    </body>
    </html>
</xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

你试过解决吗?如果是,你可以分享如何,我们可以指导你的路径,如果不是

XSLT并不是那么难,它基本上是一个用于XML的CSS,你应该看看这里的初学者:http://www.w3schools.com/xsl/default.asp

如果您不熟悉xpath,请点击此处: http://www.w3schools.com/xpath/

然后针对更具体的答案优化您的问题。