我的XML表显示但不填充文本

时间:2014-12-08 19:48:09

标签: xml xslt

这是.XML文件

这是提供的,我正在尝试创建.XSL文件,以使用此(XML)文件的信息填充表。

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="sigmod.xsl"?>
<!DOCTYPE SigmodRecord [
<!ELEMENT SigmodRecord (issue)* >
<!ELEMENT issue (volume,number,articles) >
<!ELEMENT volume (#PCDATA)>
<!ELEMENT number (#PCDATA)>
<!ELEMENT articles (article)* >
<!ELEMENT article (title,initPage,endPage,authors) >
<!ELEMENT title (#PCDATA)>
<!ELEMENT initPage (#PCDATA)>
<!ELEMENT endPage (#PCDATA)>
<!ELEMENT authors (author)* >
<!ELEMENT author (#PCDATA)>
<!ATTLIST author position CDATA #IMPLIED>
]>

<SigmodRecord>
<issue>
<volume>11</volume>
<number>1</number>
<articles>
 <article>

<title>Annotated Bibliography on Data Design.</title>
<initPage>45</initPage>
<endPage>77</endPage>
      <authors>
<author position="00">Anthony I. Wasserman</author>
<author position="01">Karen Botnich</author>

      </authors>
 </article>

 <article>
<title>Architecture of Future Data Base Systems.</title>
<initPage>30</initPage>
<endPage>44</endPage>
      <authors>

<author position="00">Lawrence A. Rowe</author>
<author position="01">Michael Stonebraker</author>
      </authors>
 </article>

 <article>
<title>Database Directions III Workshop Review.</title>
<initPage>8</initPage>

<endPage>8</endPage>
      <authors>
<author position="00">Tom Cook</author>
      </authors>
 </article>

</articles>
</issue>
</SigmodRecord>

这是.XSL文件

我不确定我的Xpath是否正确,我猜它不是,这就是为什么我的桌子不会填充,但由于某种原因我似乎无法弄清楚...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="SigmodRecord">
<html>  
<body>  
<h2>Articles</h2>    
<table border="1">      
<tr bgcolor="#9acd32">        
<th>Volume</th>        
<th>Number</th> 
<th>Title</th>  
<th>Start Page</th> 
<th>End Page</th>   
<th>Page Length</th>      
</tr>      
<xsl:for-each select="SigmodRecord/issue/articles/article">      
<tr>        
<td><xsl:value-of select="issue/volume"/></td>        
<td><xsl:value-of select="issue/number"/></td>  
<td><xsl:value-of select="title"/></td> 
<td><xsl:value-of select="initPage"/></td>  
<td><xsl:value-of select="endPage"/></td>   
<td><xsl:value-of select="((endPage)-(initPage))"/></td>      
</tr>      
</xsl:for-each>   
</table>  
</body>
</html>
</xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:0)

  

我不确定我的Xpath是否正确,我猜它不是,那就是   为什么我的桌子不会填充

这是一个准确的猜测。以这种方式进行测试:

<xsl:template match="/">
    <html>  
        <body>  
        <h2>Articles</h2>    
        <table border="1">      
            <tr bgcolor="#9acd32">        
                <th>Volume</th>        
                <th>Number</th> 
                <th>Title</th>  
                <th>Start Page</th> 
                <th>End Page</th>   
                <th>Page Length</th>      
            </tr>      
            <xsl:for-each select="SigmodRecord/issue/articles/article">      
                <tr>        
                    <td><xsl:value-of select="../../volume"/></td>        
                    <td><xsl:value-of select="../../number"/></td>  
                    <td><xsl:value-of select="title"/></td> 
                    <td><xsl:value-of select="initPage"/></td>  
                    <td><xsl:value-of select="endPage"/></td>   
                    <td><xsl:value-of select="endPage - initPage"/></td>      
                </tr>      
            </xsl:for-each>   
        </table>  
        </body>
    </html>
</xsl:template>

注意:
1.如果您在浏览器中运行它,那么您正在使用XSLT 1.0;
2.没有必要使用括号来进行减法。

答案 1 :(得分:0)

您的模板已匹配SigmodRecord,因此您可以将for-each更改为

<xsl:for-each select="issue/articles/article">      
  <tr>        
    <td><xsl:value-of select="parent::articles/preceding-sibling::volume"/></td>        
    <td><xsl:value-of select="parent::articles/preceding-sibling::number"/></td>  
    <td><xsl:value-of select="title"/></td> 
    <td><xsl:value-of select="initPage"/></td>  
    <td><xsl:value-of select="endPage"/></td>   
    <td><xsl:value-of select="((endPage)-(initPage))"/></td>      
    </tr>      
</xsl:for-each> 

通过此调整后,将生成输出:

<html>
 <body>
  <h2>Articles</h2>
  <table border="1">
     <tr bgcolor="#9acd32">
        <th>Volume</th>
        <th>Number</th>
        <th>Title</th>
        <th>Start Page</th>
        <th>End Page</th>
        <th>Page Length</th>
     </tr>
     <tr>
        <td>11</td>
        <td>1</td>
        <td>Annotated Bibliography on Data Design.</td>
        <td>45</td>
        <td>77</td>
        <td>32</td>
     </tr>
     <tr>
        <td>11</td>
        <td>1</td>
        <td>Architecture of Future Data Base Systems.</td>
        <td>30</td>
        <td>44</td>
        <td>14</td>
     </tr>
     <tr>
        <td>11</td>
        <td>1</td>
        <td>Database Directions III Workshop Review.</td>
        <td>8</td>
        <td>8</td>
        <td>0</td>
     </tr>
  </table>
  </body>
</html>

由于valueid是每个article的父元素,因此可以使用

选择它们
<xsl:value-of select="parent::articles/preceding-sibling::volume"/>

xsl:for-each循环中。