简体   繁体   English

XSLT:选择一个idref并找到corr。 元件

[英]XSLT: select one idref and find corr. element

I have an xml file about movies that looks like this (short version) 我有一个关于电影的xml文件,看起来像这样(简短版本)

    <movie id="movie_tt0004994">
       <title>Bound on the Wheel </title>
       <stars idref="star_nm0933368 star_nm0913085 star_nm0151606"/>
    </movie>
    <star id="star_nm0933368">
       <name>Elsie Jane Wilson</name>
    </star>

I want to transform this xml into html, using xslt. 我想使用xslt将这个xml转换成html。 The html should be a table with the movie title in the first column, and the star NAMES (which are at most 3) in the following three columns. html应该是一个表,其中的电影标题在第一列中,而星名(最多3个)在接下来的三列中。

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:template match="/">
 <html>
 <body>
 <h2>movie list</h2>
 <table border="1">
 <th>Title</th>
 <th colspan="3">Stars</th>
 </tr>
 <xsl:for-each select="IMDb/movie">
 <tr>
 <td><xsl:value-of select="title" /></td>
 <xsl:for-each select="stars/@idref">
 <xsl:variable name="curr_ref" select="."/>
 <td><xsl:value-of select="//IMDb/star[@id=$curr_ref]/name"/></td>
 </xsl:for-each>    
 </tr>
 </xsl:for-each>    
 </table>
 </font>
 </body>
 </html>
 </xsl:template>
 </xsl:transform>

The problem is that it only works for movies with one star. 问题在于它仅适用于一颗星的电影。 If there are multiple star ids in stars (like in the movie in the given part of my xml) then the corresponding columns in my table remain empty. 如果星星中有多个星星ID(例如,在xml中给定部分的电影中),则表中的相应列将保持为空。 I think this is because the line then makes curr_ref one long string of all the idrefs instead of three seperate ones. 我认为这是因为该行然后使curr_ref成为所有idref的一个长字符串,而不是三个单独的字符串。 How should I go about this? 我应该怎么做?

Assuming XSLT 2.0 you can use 假设您可以使用XSLT 2.0

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

 <xsl:key name="star" match="star" use="@id"/>

 <xsl:template match="/">
 <html>
 <body>
 <h2>movie list</h2>
 <table border="1">
 <tr>
 <th>Title</th>
 <th colspan="3">Stars</th>
 </tr>
 <xsl:for-each select="IMDb/movie">
 <tr>
 <td><xsl:value-of select="title" /></td>
 <xsl:for-each select="for $ref in tokenize(stars/@idref, '\s+') return key('star', $ref)">
 <td><xsl:value-of select="name"/></td>
 </xsl:for-each>    
 </tr>
 </xsl:for-each>    
 </table>
 </body>
 </html>
 </xsl:template>

 </xsl:transform>

Assuming XSLT 1.0 (or later) and DTD support you can use 假设您可以使用XSLT 1.0(或更高版本)和DTD支持

<!DOCTYPE IDMb [
<!ATTLIST star
  id ID #REQUIRED>
<!ATTLIST stars
  idref IDREFS #REQUIRED>
]>

<IMDb>
<movie id="movie_tt0004994">
       <title>Bound on the Wheel </title>
       <stars idref="star_nm0933368 star_nm0913085 star_nm0151606"/>
    </movie>
    <star id="star_nm0933368">
       <name>Elsie Jane Wilson</name>
    </star>
</IMDb>

and

 <xsl:template match="/">
 <html>
 <body>
 <h2>movie list</h2>
 <table border="1">
 <tr>
 <th>Title</th>
 <th colspan="3">Stars</th>
 </tr>
 <xsl:for-each select="IMDb/movie">
 <tr>
 <td><xsl:value-of select="title" /></td>
 <xsl:for-each select="id(stars/@idref)">
 <td><xsl:value-of select="name"/></td>
 </xsl:for-each>    
 </tr>
 </xsl:for-each>    
 </table>
 </body>
 </html>
 </xsl:template>

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

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