2017-04-11 171 views
0

我需要以特定順序獲取屬性,所以我在特定的一組名稱上使用循環。我想做一些像@ $屬性或類似@的東西。但只是使用。給我的名字本身,而不是我想要的屬性的價值。使用變量作爲屬性名稱

基本上不是每行都說「標題」和「藝術家」,我想要的是實際的標題和藝術家。

的hello.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<results> 
    <r title="Empire Burlesque" artist="Bob Dylan" /> 
    <r title="Hide your heart" artist="Bonnie Tyler" /> 
    <r title="Greatest Hits" artist="Dolly Parton" /> 
</results> 

hello.xsl:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template name="boxNames"> <!-- not used as template --> 
    <name>title</name> 
    <name>artist</name> 
</xsl:template> 
<xsl:template match="/"> 
<xsl:variable name="boxNames" select="document('')/xsl:stylesheet/xsl:template[@name='boxNames']/name" /> 
<html> 
<body> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
    <tr bgcolor="#9acd32"> 
     <xsl:for-each select="$boxNames"> 
     <th style="text-align:left"><xsl:value-of select="." /></th> 
     </xsl:for-each> 
    </tr> 
    <xsl:for-each select="results/r"> 
    <tr> 
     <xsl:for-each select="$boxNames"> 
     <td><xsl:value-of select="."/></td> 
     </xsl:for-each> 
    </tr> 
    </xsl:for-each> 
    </table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

輸出:

<html> 
    <body> 
     <h2>My CD Collection</h2> 
     <table border="1"> 
     <tr bgcolor="#9acd32"> 
      <th style="text-align:left">title</th> 
      <th style="text-align:left">artist</th> 
     </tr> 
     <tr> 
      <td>title</td> 
      <td>artist</td> 
     </tr> 
     <tr> 
      <td>title</td> 
      <td>artist</td> 
     </tr> 
     <tr> 
      <td>title</td> 
      <td>artist</td> 
     </tr> 
     </table> 
    </body> 
</html>Execution time: 88.941623ms 

回答

0

我不明白爲什麼你不能簡單地硬編碼的順序例如,您想將其放入樣式表中的列,例如:

XSLT 1.0

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

<xsl:template match="/results"> 
    <html> 
     <body> 
      <h2>My CD Collection</h2> 
      <table border="1"> 
       <!-- header --> 
       <tr> 
        <th>title</th> 
        <th>artist</th> 
       </tr> 
       <!-- body --> 
       <xsl:for-each select="r"> 
        <tr> 
         <td> 
          <xsl:value-of select="@title"/> 
         </td> 
         <td> 
          <xsl:value-of select="@artist"/> 
         </td> 
        </tr> 

       </xsl:for-each> 
      </table> 
     </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

但如果你真的想在一個單獨的列表,列舉列名,試試這樣:

XSLT 1.0

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

<xsl:variable name="columns"> 
    <name>title</name> 
    <name>artist</name> 
</xsl:variable> 

<xsl:variable name="column-names" select="document('')/*/xsl:variable[@name='columns']/name" /> 

<xsl:template match="/results"> 
    <html> 
     <body> 
      <h2>My CD Collection</h2> 
      <table border="1"> 
       <!-- header --> 
       <tr> 
        <xsl:for-each select="$column-names"> 
         <th> 
          <xsl:value-of select="." /> 
         </th> 
        </xsl:for-each> 
       </tr> 
       <!-- body --> 
       <xsl:for-each select="r"> 
        <xsl:variable name="row" select="."/> 
        <tr> 
         <xsl:for-each select="$column-names"> 
          <td> 
           <xsl:value-of select="$row/@*[name()=current()]"/> 
          </td> 
         </xsl:for-each> 
        </tr> 
       </xsl:for-each> 
      </table> 
     </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet>