【问题标题】:XML to HTML transformation with sorting带排序的 XML 到 HTML 转换
【发布时间】:2012-11-27 14:20:21
【问题描述】:

以下是我需要转换的原始 XML 文件。这是一个音乐专辑记录列表,我必须将其转换为 HTML 表格,其中专辑按发行年份升序排序,我需要与每个单独的记录元素中包含的流派和记录标签元素建立联系一个 id 参考并将它们输出到表中以及每个专辑。

在第二次转换中,我需要做的基本相同,但在这种情况下,输出应该只包括摇滚和流行音乐流派的专辑。

    <?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="zadacha1.xsl" ?>
<record_collection>
    <genres>
        <genre id="1">Rock</genre>
        <genre id="2">Pop</genre>
        <genre id="3">Disco</genre>
        <genre id="4">Rap</genre>
        <genre id="5">Electronic</genre>
        <genre id="6">Country</genre>
    </genres>

    <record_labels>
        <record_label id="1">DGC Records</record_label>
        <record_label id="2">Atlantic</record_label>
        <record_label id="3">Epic</record_label>
        <record_label id="4">Warner Bros.</record_label>
        <record_label id="5">EMI</record_label>
        <record_label id="6">Columbia</record_label>
    </record_labels>

    <records>
        <record>
            <artist>Nirvana</artist>
            <genreID>1</genreID>
            <album>Nevermind</album>
            <year_of_issue>1992</year_of_issue>
            <record_labelID>1</record_labelID>
        </record>

        <record>
            <artist>Twisted Sister</artist>
            <genreID>1</genreID>
            <album>Stay Hungry</album>
            <year_of_issue>1984</year_of_issue>
            <record_labelID>2</record_labelID>
        </record>   

        <record>
            <artist>Michael Jackson</artist>
            <genreID>2</genreID>
            <album>Thriller</album>
            <year_of_issue>1982</year_of_issue>
            <record_labelID>3</record_labelID>
        </record>   

        <record>
            <artist>Bee Gees</artist>
            <genreID>3</genreID>
            <album>Spirits Having Flown</album>
            <year_of_issue>1979</year_of_issue>
            <record_labelID>4</record_labelID>
        </record>   

        <record>
            <artist>Ice-T</artist>
            <genreID>4</genreID>
            <album>O.G. Original Gangster</album>
            <year_of_issue>1991</year_of_issue>
            <record_labelID>4</record_labelID>
        </record>

        <record>
            <artist>Kraftwerk</artist>
            <genreID>5</genreID>
            <album>Computer World</album>
            <year_of_issue>1981</year_of_issue>
            <record_labelID>5</record_labelID>
        </record>

        <record>
            <artist>Johnny Cash</artist>
            <genreID>6</genreID>
            <album>Man in Black</album>
            <year_of_issue>1971</year_of_issue>
            <record_labelID>6</record_labelID>
        </record>

    </records>
</record_collection>

我可以管理的是输出一个没有流派和记录标签元素的未排序表,因为它们通过每个单独记录中的 id 链接,我不知道如何到达 id 并输出后面的文本ID。这就是我到目前为止所拥有的 XSLT 转换。

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

    <xsl:template match="/record_collection/records">
        <html>
         <head>
            <title>Records Collection</title>
         </head>
         <body>
            <h1>Records Collection</h1>
            <table border="1">
                <tr>
                    <th>Genre</th>
                    <th>Artist</th>
                    <th>Album</th>
                    <th>Year</th>
                    <th>Label</th>
                </tr>

                <xsl:apply-templates select="record" />
            </table>
            <xsl:call-template name="footer" />
         </body>
        </html>
    </xsl:template>

    <xsl:template match="record">
        <tr>
            <td>?</td>
            <td><xsl:value-of select="artist" /></td>
            <td><xsl:value-of select="album" /></td>
            <td><xsl:value-of select="year_of_issue" /></td>
            <td>?</td>
        </tr>
    </xsl:template>


    <xsl:template name="footer">
        <div style="margin: 20px 0; padding: 10px; background-color: #efefef; ">
            Record Collection
        </div>
    </xsl:template>

</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    你只需要使用:

    <xsl:apply-templates select="record">
        <xsl:sort select="year_of_issue" order="ascending" data-type="number"/>
    </xsl:apply-templates>
    

    而不是

    <xsl:apply-templates select="record"/>
    

    表格将按发行年份升序排列

    【讨论】:

      【解决方案2】:

      这是使用xsl:key 查找您的流派和唱片公司的好地方

      <?xml version="1.0"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
          <xsl:template match="/">
              <xsl:apply-templates select="/record_collection/records"></xsl:apply-templates>
          </xsl:template>
      
          <xsl:key name="genre-lookup" match="/record_collection/genres/genre" use="@id"/>
          <xsl:key name="record_label" match="/record_collection/record_labels/record_label" use="@id"/>
      
          <xsl:template match="records">
              <html>
                  <head>
                      <title>Records Collection</title>
                  </head>
                  <body>
                      <h1>Records Collection</h1>
                      <table border="1">
                          <tr>
                              <th>Genre</th>
                              <th>Artist</th>
                              <th>Album</th>
                              <th>Year</th>
                              <th>Label</th>
                          </tr>
      
                          <xsl:apply-templates select="record" />
                      </table>
                      <xsl:call-template name="footer" />
                  </body>
              </html>
          </xsl:template>
      
          <xsl:template match="record">
              <tr>
                  <td>
                      <xsl:value-of select="key('genre-lookup',genreID)" />
                  </td>
                  <td>
                      <xsl:value-of select="artist" />
                  </td>
                  <td>
                      <xsl:value-of select="album" />
                  </td>
                  <td>
                      <xsl:value-of select="year_of_issue" />
                  </td>
                  <td>
                      <xsl:value-of select="key('record_label',record_labelID)" />
                  </td>
              </tr>
          </xsl:template>
      
      
          <xsl:template name="footer">
              <div style="margin: 20px 0; padding: 10px; background-color: #efefef; ">
                  Record Collection
              </div>
          </xsl:template>
      
      </xsl:stylesheet>
      

      如果您只想按摇滚或流行音乐过滤,请调整您的apply-templates 以仅按genreId = 1 / 2 过滤:

      <xsl:apply-templates select="record[genreID=1 or genreID=2]" />
      

      对原始 xslt 的一个小改动是捕获根目录,以防止将默认处理指令应用于文档。

      【讨论】:

      • 非常感谢您的帮助好友 :) !!!还有一件事 - 转换后,文档顶部留下了“流派”和“唱片公司”类别的条目。如何阻止它们显示在转换后的文档中?
      • @GrigorPetrov - 这是default built in templates 的影响。将apply-templates 的“根”模板添加到您感兴趣的节点可以解决这个问题(我已经在我的回答中这样做了)
      猜你喜欢
      • 2013-06-14
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      相关资源
      最近更新 更多