使用concat显示元素XSLT

时间:2019-01-12 10:34:02

标签: xml xslt xslt-1.0

我正在尝试获得类似的东西,但是对于所有类型和电影,不仅是第一个

A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation A Beautiful Mind Animation

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="films3.xsl"?>
<list>

            List:

        <movie cinema="yes">A Beautiful Mind</movie>

        <movie cinema="yes">Avatar</movie>

        <movie cinema="yes">Cast Away</movie>

        <movie cinema="no">Fight Club</movie>

        <movie cinema="no">Forest Gump</movie>

        <movie cinema="yes">Gladiator</movie>

        <movie cinema="yes">Inception</movie>

        <movie cinema="yes">Inglourious Basterds</movie>

        <movie cinema="yes">Intouchables</movie>

        <movie cinema="yes">Léon</movie>

        <movie cinema="no">Matrix</movie>

        <movie cinema="yes">Pirates of the Caribbean: The Curse of the Black Pearl</movie>

        <movie cinema="yes">Pulp Fiction</movie>

        <movie cinema="yes">Requiem for a Dream</movie>

        <movie cinema="yes">Saving Private Ryan</movie>

        <movie cinema="yes">Schindler's List</movie>

        <movie cinema="no">Se7en</movie>

        <movie cinema="yes">Seven Pounds</movie>

        <movie cinema="no">Shrek</movie>

        <movie cinema="yes">Shutter Island </movie>

        <movie cinema="yes">Slumdog Millionaire</movie>

        <movie cinema="yes">The Godfather</movie>

        <movie cinema="yes">The Green Mile</movie>

        <movie cinema="yes">The Hangover</movie>

        <movie cinema="yes">The Lord of the Rings: The Fellowship of the Ring</movie>

        <movie cinema="yes">The Pianist</movie>

        <movie cinema="yes">The Shawshank Redemption</movie>

        <movie cinema="yes">The Silence of the Lambs</movie>

        <movie cinema="yes">The Sixth Sense</movie>

        <movie cinema="yes">Titanic </movie>



        Genres' list:

        <genre>Animation</genre>

            <genre>Biography</genre>

            <genre>Biography, drama</genre>

            <genre>Drama</genre>

            <genre>Drama</genre>

            <genre>Drama</genre>

            <genre>Drama</genre>

            <genre>Drama</genre>

            <genre>Drama</genre>

            <genre>Drama</genre>

            <genre>Drama</genre>

            <genre>History</genre>

            <genre>War</genre>

            <genre>War</genre>

            <genre>War</genre>

            <genre>Fantasy</genre>

            <genre>Thriller</genre>

            <genre>Thriller</genre>

            <genre>Comedy</genre>

            <genre>Crime</genre>

            <genre>Crime</genre>

            <genre>Romance</genre>

            <genre>Adventure</genre>

            <genre>Sci-Fi</genre>

            <genre>Sci-Fi</genre>

            <genre>Drama</genre>

            <genre>Thriller</genre>

            <genre>Thriller</genre>

            <genre>Thriller</genre>

            <genre>War</genre>


    How many movies?: 30
</list>

这是我用来获取所需的.txt的.xsl:

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

 <xsl:variable name="movie">
 <xsl:apply-templates select="/list/movie" />
 </xsl:variable>

 <xsl:variable name="gen">
 <xsl:apply-templates select="/list/genre"/>
 </xsl:variable>



 <!--This one below is used only-->
 <xsl:template match="/">
 <xsl:for-each select="/list/movie">
  <xsl:value-of select="concat(/list/movie,'    ',/list/genre)"/>
  <xsl:text>
 </xsl:text>
 </xsl:for-each>
 <!--><xsl:value-of select="concat($gen,'                 ',$movie)"/>-->

 </xsl:template>


 <xsl:template match="/movie">
 <xsl:for-each select="movie">
  <xsl:value-of select="movie"/>
 <xsl:text>


  </xsl:text>
  </xsl:for-each>
  </xsl:template>

   </xsl:stylesheet>

代码中提到的解决方案很少,每个解决方案都在出错,几天以来,我一直在努力完成此工作,所以最终我决定来这里寻求帮助。我只需要一些建议。

1 个答案:

答案 0 :(得分:0)

正如我在对您的问题的评论中说的那样,除了常见的计数之外,我没有看到电影与流派之间的任何联系。

如果您只想并排列出它们,则可以执行以下操作:

XSLT 1.0

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

<xsl:template match="/list">
    <xsl:for-each select="movie">
        <xsl:variable name="i" select="position()" />
        <xsl:value-of select="."/>
        <xsl:text>&#9;</xsl:text>
        <xsl:value-of select="../genre[$i]"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>