2013-04-25 131 views
1

對於XSLT樣式表,我很新。我已經和他們做了一些非常基礎的工作,現在已經得到了一個測試我的能力的項目。我甚至不認爲我以正確的方式提出這個問題。XSLT:根據屬性選擇節點和屬性

下面是一組XML數據,代表我正在使用的數據。它的廢話數據...

<?xml version="1.0" encoding="utf-8"?> 
<catalog_new> 
    <catalog_old> 
     <catalog_newold> 
      <final> 
       <cd_info title="Empire Burlesque" artist="Dylan" /> 
       <store name="BestBuy" /> 
        <sales thismonth="500"/> 
        <returns thismonth="10"/> 
       <store name="Target" /> 
        <sales thismonth="500"/> 
        <returns thismonth="10"/> 
      </final> 
      <final> 
       <cd_info title="Stand" artist="REM" /> 
       <store name="BestBuy" /> 
        <sales thismonth="1000"/> 
        <returns thismonth="20"/> 
       <store name="Target" /> 
        <sales thismonth="530"/> 
        <returns thismonth="50"/> 
      </final> 
     </catalog_newold> 
    </catalog_old> 
    </catalog_new> 

我需要做的是選擇每個最終/ cd_info項目,然後只需選擇店裏記錄下名稱=百思買併爲每個銷售和回報這個月值。

我想產生一個表,看起來像:

CDName   Artist   Store   SalesThisMonth Returns 
Stand   R.E.M   Best Buy   500    10 

這是我到:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="html" indent="yes" encoding = "utf-8" standalone = "yes"/> 

    <xsl:template match="/"> 

    <html> 
     <head> 
     <meta charset="utf-8" /> 
     <title></title> 
     </head> 
     <body> 

     <table border="1" style="width: 100%;"> 
      <tr> 
      <th>Albumn Name</th> 
      <th>Artist</th> 
      <th>Store</th> 
      </tr> 

      <xsl:for-each select ="//catalog_new"> 
      <xsl:for-each select="catalog_old"> 
       <xsl:for-each select="catalog_newold"> 
       <xsl:for-each select="final"> 
        <xsl:for-each select="cd_info"> 
        <tr> 
         <td> 
         <xsl:value-of select="@title"/> 
         </td> 
         <td> 
         <xsl:value-of select="@artist"/> 
         </td> 
        </tr> 
        </xsl:for-each> 
       </xsl:for-each> 
       </xsl:for-each> 
      </xsl:for-each> 
      </xsl:for-each> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

我已經結束了刪除每次嘗試我做了.....我只是偏離軌道,還是我在正確的軌道上?

在此先感謝。

回答

2

這個怎麼樣:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="html" indent="yes" encoding="utf-8" standalone = "yes"/> 
    <xsl:variable name="storeMapNf"> 
    <map from="BestBuy" to="Best Buy" /> 
    </xsl:variable> 
    <xsl:variable name="storeMapping" select="msxsl:node-set($storeMapNf)/*" /> 

    <xsl:template match="/"> 

    <html> 
     <head> 
     <meta charset="utf-8" /> 
     <title></title> 
     </head> 
     <body> 

     <table border="1" style="width: 100%;"> 
      <tr> 
      <th>Albumn Name</th> 
      <th>Artist</th> 
      <th>Store</th> 
      <th>SalesThisMonth</th> 
      <th>Returns</th> 
      </tr> 

      <xsl:apply-templates select="//final/store[@name = 'BestBuy']" /> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="final/store"> 
    <xsl:variable name="cdi" select="../cd_info" /> 
    <xsl:variable name="storeMap" 
        select="$storeMapping[@from = current()/@name]" /> 
    <tr> 
     <td> 
     <xsl:value-of select="$cdi/@title" /> 
     </td> 
     <td> 
     <xsl:value-of select="$cdi/@artist" /> 
     </td> 
     <td> 
     <xsl:value-of select="$storeMap/@to | 
           @store[not($storeMap)]"/> 
     </td> 
     <td> 
     <xsl:value-of select="following-sibling::sales/@thismonth"/> 
     </td> 
     <td> 
     <xsl:value-of select="following-sibling::returns/@thismonth"/> 
     </td> 
    </tr> 
    </xsl:template> 
</xsl:stylesheet> 

當你的樣品輸入運行,其結果是:

<html> 
    <head> 
    <META http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <meta charset="utf-8"> 
    <title></title> 
    </head> 
    <body> 
    <table border="1" style="width: 100%;"> 
     <tr> 
     <th>Albumn Name</th> 
     <th>Artist</th> 
     <th>Store</th> 
     <th>SalesThisMonth</th> 
     <th>Returns</th> 
     </tr> 
     <tr> 
     <td>Empire Burlesque</td> 
     <td>Dylan</td> 
     <td>Best Buy</td> 
     <td>500</td> 
     <td>10</td> 
     </tr> 
     <tr> 
     <td>Stand</td> 
     <td>REM</td> 
     <td>Best Buy</td> 
     <td>1000</td> 
     <td>20</td> 
     </tr> 
    </table> 
    </body> 
</html> 
+0

它真的接近我一直在尋找的 - 我想上面的代碼和它只有返回單行(但它返回我期待的方式)除此之外,它的工作。爲什麼它不會給我第二個記錄?我最不明白的部分是storeMap變量和select。謝謝你的時間... – 2013-04-25 03:28:43

+0

忽略之前的評論。我意識到我沒有保存帶有額外的行的文件。 DOH!感謝你的協助。 – 2013-04-25 03:35:44

0

這種轉變

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/*"> 
    <html> 
     <head> 
     <meta charset="utf-8" /> 
     <title></title> 
     </head> 
     <body> 

     <table border="1" style="width: 100%;"> 
      <tr> 
      <th>Albumn Name</th> 
      <th>Artist</th> 
      <th>Store</th> 
      <th>SalesThisMonth</th> 
      <th>Returns</th> 
      </tr> 
      <xsl:apply-templates/> 
     </table> 
    </body> 
    </html> 
</xsl:template> 

<xsl:template match="store[@name='BestBuy']"> 
    <tr> 
    <xsl:apply-templates select= 
    "@name| ../cd_info/@* | following-sibling::*[not(position()>2)]/@thismonth"/> 
    </tr> 
</xsl:template> 

<xsl:template match="@*"> 
    <td><xsl:value-of select="."/></td> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的應用XML文檔

<catalog_new> 
    <catalog_old> 
     <catalog_newold> 
      <final> 
       <cd_info title="Empire Burlesque" artist="Dylan" /> 
       <store name="BestBuy" /> 
       <sales thismonth="500"/> 
       <returns thismonth="10"/> 
       <store name="Target" /> 
       <sales thismonth="500"/> 
       <returns thismonth="10"/> 
      </final> 
      <final> 
       <cd_info title="Stand" artist="REM" /> 
       <store name="BestBuy" /> 
       <sales thismonth="1000"/> 
       <returns thismonth="20"/> 
       <store name="Target" /> 
       <sales thismonth="530"/> 
       <returns thismonth="50"/> 
      </final> 
     </catalog_newold> 
    </catalog_old> 
</catalog_new> 

產生想要的,正確的結果:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

     <meta charset="utf-8"> 
     <title></title> 
    </head> 
    <body> 
     <table border="1" style="width: 100%;"> 
     <tr> 
      <th>Albumn Name</th> 
      <th>Artist</th> 
      <th>Store</th> 
      <th>SalesThisMonth</th> 
      <th>Returns</th> 
     </tr> 
     <tr> 
      <td>Empire Burlesque</td> 
      <td>Dylan</td> 
      <td>BestBuy</td> 
      <td>500</td> 
      <td>10</td> 
     </tr> 
     <tr> 
      <td>Stand</td> 
      <td>REM</td> 
      <td>BestBuy</td> 
      <td>1000</td> 
      <td>20</td> 
     </tr> 
     </table> 
    </body> 
</html>