2013-04-25 3 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 элементов, а затем просто выбрать магазин запись, где имя = Best Buy и выбрать значение в этом месяце для каждого продаж и возвратов ,

Я пытаюсь создать таблицу, которая выглядит следующим образом:

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 и выбирает. Спасибо за ваше время ... –

+0

Игнорировать предыдущий комментарий. Я понял, что не сохранил файл с дополнительной строкой. DOH! Спасибо за вашу помощь. –

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>