首页 > 解决方案 > XSLT 比较属性和值

问题描述

嘿,我有一个关于作业的快速问题

我的 xml 中有一个列表,其中包含这样的电影商店的类型:

......

<genre ID="G3" name="Action"/>
<genre ID="G4" name="Romance"/>
<genre ID="G5" name="Sci-fi">

....和更多

和一个电影列表,它指的是这样的流派:

<movie ID = "F6" titel ="Blood Work" year = "2002" length = "109">
        <!-- Genres --> 
                <genre ID ="G11" />
                <genre ID ="G7" />

......

在图片中你可以看到我到目前为止得到了什么。而不是流派 ID(G5 和 G3)等等,我想要它的名称。这就是我到目前为止感到疲倦的原因:

<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:template match="/">
  <html>
  <body>
        <xsl:apply-templates select="*"/>
  </body>
  </html>
</xsl:template>

<xsl:template match="movieshop">
<table border="1">
    <tr bgcolor="#9acd32">
   <!--Strukturierung der Tabelle -->
      <th>ID</th>
      <th>Title</th>
      <th>Description</th>
      <th>Genre</th>
      <th>Length (in Minuten)</th>
      <th>Year</th>
    </tr>
   <!--fill table with the elements movies -->
     <xsl:for-each select="//movieshop/movies/movie">
    <tr>
     <!-- ID of the movie  -->
      <td><xsl:value-of select="@ID"/></td>
     <!-- titel of the movie  -->
      <td><xsl:value-of select="@titel"/></td>
      <!-- Description of the movie  -->
      <td><xsl:value-of select="details/@inhalt"/></td>
       <!-- Genre of the movie  -->
    <td> 
    <xsl:for-each select="genre/@ID">
                    <xsl:call-template name="resultGenres">
                        <xsl:with-param name="id" select="."/>
                    </xsl:call-template>
    </xsl:for-each> 
    </td>
       <!-- Length -->
      <td><xsl:value-of select="@length"/></td>
       <!-- Year -->
      <td><xsl:value-of select="@year"/></td>  
    </tr>
 </xsl:for-each>
</table>
</xsl:template>

<xsl:template name="resultGenres">
            <xsl:param name="id"/>
    <!--Just testing what id is -->
            <p>Genre: <xsl:value-of select = "$id" /></p>
            <xsl:for-each select="//movieshop/genres/genre[ID=$id]">
                <xsl:call-template name="resultGenre"/>
            </xsl:for-each>
</xsl:template>

<xsl:template name="resultGenre">
        <xsl:value-of select="//movieshop/genres/genre/@name"/>
        <br/>
</xsl:template>



</xsl:stylesheet>

错误必须在这里的某个地方:

<xsl:for-each select="//movieshop/genres/genre[ID=$id]">

即使 G1 等存在于我的列表中,也没有结果集。如果我在代码中拼错了一些东西,我很抱歉。尝试将所有单词更改为英语。先谢谢了

我的结果预览

标签: xmlxslt

解决方案


推荐阅读