简体   繁体   English

xml的xsl语法

[英]xsl syntax for an xml

I have the following structure for a movies xml file 电影xml文件具有以下结构

<movies>
 <movie>
    <genre>Drama</genre>
    <genre>Thriller</genre>
 </movie>
 ....
</movies>

Shouldn't this snippet for selecting every value of genre for each movie tag work ? 为每个电影标签选择每个体裁值的代码片段都行不通吗?

<xsl:template match="/">
<html>
<body>
<h2>MOVIES</h2>
<table border="1">
<tr bgcolor="#9acd32">
  <th>Genre</th>
</tr>
<xsl:for-each select="movies/movie">
<tr>
  <td>
  <xsl:for-each select="movies/movie/genre">

          <xsl:value-of select="genre"/>

</xsl:for-each>
  </td>

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

</xsl:stylesheet>

It shows nothing as ouput. 它没有显示出输出。 Can anyone please point out the mistake ? 任何人都可以指出错误吗?

It's not working because of the selector in the nested xsl:for-each : the outer one iterates through movies/movie , so in the inner one you already are in a movie element, but you're selecting movies/movie/genre ; 由于嵌套的xsl:for-each中的选择器,它不起作用:外部的一个遍历movies/movie ,因此在内部的一个中,您已经在movie元素中,但是您正在选择movies/movie/genre that means you're looking for /movies/movie/movies/movie/genre . 这意味着您正在寻找/movies/movie/movies/movie/genre The xsl:value-of uses an incorrect selector as well. xsl:value-of使用了错误的选择器。

The code should be (untested): 该代码应(未试用):

<xsl:for-each select="movies/movie">
<tr>
  <td>
  <!-- context element here is movie -->
  <xsl:for-each select="genre">
      <!-- context element here is genre -->
      <xsl:value-of select="."/>
      <!-- Separate genres with a comma -->
      <xsl:if test="not(position() = last())">, </xsl:if>
  </xsl:for-each>
  </td>
</tr>
</xsl:for-each>

By the way (off-topic), the bgcolor attribute was deprecated in HTML 4 (a long time ago) :) You should use style instead: 顺便说一句(离题), bgcolor属性在HTML 4中已经过时了(很久以前):)您应该改用style

<tr style="background-color: #9acd32;">

While sierrasdetandil's answer already gave you the solution for your problem at hand I'd like to provide an alternative using apply-templates instead of for-each : 尽管sierrasdetandil的答案已经为您提供了解决您手头问题的方法,但我想提供一个替代方法,使用apply-templates代替for-each

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />

    <xsl:template match="/">
        <html>
            <body>
                <h2>MOVIES</h2>
                    <table>
                        <tr><th>Genre</th></tr>
                        <xsl:apply-templates />
                    </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="movies">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="movie">
        <tr>
            <td>
                <xsl:apply-templates />
            </td>
        </tr>
    </xsl:template>

    <xsl:template match="genre">
        <xsl:value-of select="." />
    </xsl:template>

</xsl:stylesheet>

As simple as this : 就这么简单

<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="/">
  <table><xsl:apply-templates/></table>
 </xsl:template>
 <xsl:template match="movie">
     <tr>
       <xsl:apply-templates select="node()|@*"/>
     </tr>
 </xsl:template>
 <xsl:template match="genre"><td><xsl:apply-templates/></td></xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document: 在提供的XML文档上应用此转换时:

<movies>
 <movie>
    <genre>Drama</genre>
    <genre>Thriller</genre>
 </movie>
</movies>

the wanted, correct result is produced: 所需的正确结果产生了:

<table>
   <tr>
      <td>Drama</td>
      <td>Thriller</td>
   </tr>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM