2016-10-20 36 views
0

嗨,我的XSL似乎不适合我的XML。即使我的代码非常简单,我似乎无法弄清楚问题所在。当我链接到XSL电子表格并打开文档时,我的XML表格似乎转换为原始数据。XSLT不能与我的XML一起工作

我的XSL

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="html"/> 

<xsl:template match="/"> 
<html> 
<head> 
    <title>Room Information</title> 
</head> 
<body> 
    <h1>Room information</h1> 
    <table border="1"> 
    <tr bgcolor="#9acd32"> 
    <th>TITLE</th> 
    <th>ACCOMODATION</th> 
    <th>COST</th> 
    <th>LEVEL</th> 
    <th>VIEW</th> 
    <th>ENSUITE</th> 
    <th>MAID_SERVICE</th> 
    <th>ROOM_SERVICE</th> 
    <th>LAUNDRY_SHOOT</th> 
    <th>ROOM_IMAGE</th> 
    <th>PAST_CUSTOMERS</th> 
    </tr> 
<xsl:for-each select="ROOMS/ROOM"> 
      <tr> 
       <td><xsl:value-of select="TITLE"/></td> 
       <td><xsl:value-of select="ACCOMODATION"/></td> 
       <td><xsl:value-of select="COST"/></td> 
       <td><xsl:value-of select="LEVEL"/></td> 
       <td><xsl:value-of select="VIEW"/></td> 
       <td><xsl:value-of select="ENSUITE"/></td> 
       <td><xsl:value-of select="MAID_SERVICE"/></td> 
       <td><xsl:value-of select="ROOM_SERVICE"/></td> 
       <td><xsl:value-of select="LAUNDRY_SHOOT"/></td> 
       <td><xsl:value-of select="ROOM_IMAGE"/></td> 
       <td><xsl:value-of select="PAST_CUSTOMERS"/></td> 
      </tr> 

</xsl:for-each> 

这是我的XML(DTD中刚好可以忽略不计)

?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="XSL/RoomsXSL.xsl"?> 

<!DOCTYPE ROOMS [ 
<!ELEMENT ROOM (TITLE+,ACCOMODATION,COST,LEVEL+,VIEW,ENSUITE,MAID_SERVICE,ROOM_SERVICE,LAUNDRY_SHOOT,ROOM_IMAGE+,PAST_CUSTOMERS+)> 
<!ELEMENT TITLE (#PCDATA)> 
<!ELEMENT ACCOMODATION (#PCDATA)> 
<!ELEMENT COST (#PCDATA)> 
<!ELEMENT LEVEL (#PCDATA)> 
<!ELEMENT VIEW (#PCDATA)> 
<!ELEMENT ENSUITE (#PCDATA)> 
<!ELEMENT MAID_SERVICE (#PCDATA)> 
<!ELEMENT ROOM_SERVICE (#PCDATA)> 
<!ELEMENT LAUNDRY_SHOOT (#PCDATA)> 
<!ELEMENT ROOM_IMAGE (#PCDATA)> 
<!ELEMENT PAST_CUSIMERS (#PCDATA)> 

<!ENTITY writer "Dylan Weiss"> 
<!ENTITY copyright "Horizon BnB"> 
]> 

<ROOMS> 
    <ROOM id="01"> 
    <TITLE>Top Floor 1</TITLE> 
    <ACCOMODATION>Accomodates 2 people</ACCOMODATION> 
    <COST>$700 a week.$120 every day over a week</COST> 
    <LEVEL>Top floor</LEVEL> 
    <VIEW>Includes panoramic View</VIEW> 
    <ENSUITE>Has ensuite</ENSUITE> 
    <MAID_SERVICE>Maid service included</MAID_SERVICE> 
    <ROOM_SERVICE>Includes room service</ROOM_SERVICE> 
    <LAUNDRY_SHOOT>Includes laundry shoot</LAUNDRY_SHOOT> 
    <ROOM_IMAGE>Images/LuxuryRoom1.jpg</ROOM_IMAGE> 
    <PAST_CUSTOMERS>WWW.Horizon/PastCustomers.com</PAST_CUSTOMERS> 
    </ROOM> 

    <ROOM id="02"> 
    <TITLE>Bottom floor 1</TITLE> 
    <ACCOMODATION>Accomodates 2 people</ACCOMODATION> 
    <COST>$500 a week.$100 every day over a week</COST> 
    <LEVEL>Ground floor</LEVEL> 
    <VIEW>Includes a courtyard</VIEW> 
    <ENSUITE>Has ensuite</ENSUITE> 
    <MAID_SERVICE>Maid service included</MAID_SERVICE> 
    <ROOM_SERVICE>Includes room service</ROOM_SERVICE> 
    <LAUNDRY_SHOOT>No laundry shoot</LAUNDRY_SHOOT> 
    <ROOM_IMAGE>Images/LuxuryRoom1.jpg</ROOM_IMAGE> 
    <PAST_CUSTOMERS>WWW.Horizon/PastCustomers.com</PAST_CUSTOMERS> 
    </ROOM> 

    <ROOM id="03"> 
    <TITLE>Bottom floor 2</TITLE> 
    <ACCOMODATION>Accomodates 4 people</ACCOMODATION> 
    <COST>$800 a week.$120 every day over a week</COST> 
    <LEVEL>Ground floor</LEVEL> 
    <VIEW>Includes a courtyard</VIEW> 
    <ENSUITE>Has ensuite</ENSUITE> 
    <MAID_SERVICE>Maid service included</MAID_SERVICE> 
    <ROOM_SERVICE>Includes room service</ROOM_SERVICE> 
    <LAUNDRY_SHOOT>No laundry shoot</LAUNDRY_SHOOT> 
    <ROOM_IMAGE>Images/LuxuryRoom1.jpg</ROOM_IMAGE> 
    <PAST_CUSTOMERS>WWW.Horizon/PastCustomers.com</PAST_CUSTOMERS> 
    </ROOM> 
</ROOMS> 

回答

1

我建议使用,而不是匹配模板您的for-each:根模板将拉匹配ROOMS模板,这反过来将拉动匹配ROOM

<xsl:apply-template />将小心获取所需位置的匹配模板。

如果您需要隐藏一个元素,只需声明一个空模板:<xsl:template match="ROOM" />

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="html"/> 

    <xsl:template match="/"> 
     <html> 
      <head> 
       <title>Room Information</title> 
      </head> 
      <body> 
       <h1>Room information</h1> 
       <table border="1"> 
        <tr bgcolor="#9acd32"> 
         <th>TITLE</th> 
         <th>ACCOMODATION</th> 
         <th>COST</th> 
         <th>LEVEL</th> 
         <th>VIEW</th> 
         <th>ENSUITE</th> 
         <th>MAID_SERVICE</th> 
         <th>ROOM_SERVICE</th> 
         <th>LAUNDRY_SHOOT</th> 
         <th>ROOM_IMAGE</th> 
         <th>PAST_CUSTOMERS</th> 
        </tr> 
        <xsl:apply-templates /> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="ROOMS"> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="ROOM"> 
     <tr> 
      <td> 
       <xsl:value-of select="TITLE"/> 
      </td> 
      <td> 
       <xsl:value-of select="ACCOMODATION"/> 
      </td> 
      <td> 
       <xsl:value-of select="COST"/> 
      </td> 
      <td> 
       <xsl:value-of select="LEVEL"/> 
      </td> 
      <td> 
       <xsl:value-of select="VIEW"/> 
      </td> 
      <td> 
       <xsl:value-of select="ENSUITE"/> 
      </td> 
      <td> 
       <xsl:value-of select="MAID_SERVICE"/> 
      </td> 
      <td> 
       <xsl:value-of select="ROOM_SERVICE"/> 
      </td> 
      <td> 
       <xsl:value-of select="LAUNDRY_SHOOT"/> 
      </td> 
      <td> 
       <xsl:value-of select="ROOM_IMAGE"/> 
      </td> 
      <td> 
       <xsl:value-of select="PAST_CUSTOMERS"/> 
      </td> 
     </tr> 
    </xsl:template> 

</xsl:stylesheet> 
+0

嗯,它仍然不起作用。 XML理解我已经链接了XSL,但似乎不接受将其格式化为表格的更改。 – Dylan

+0

在我的机器上在IE中运行得很好。你的示例Xsl缺少第一个“<”,但我认为这只是一个复制粘贴错误。 – Filburt

+0

它的工作原理!非常感谢! – Dylan