简体   繁体   English

XSLT-具有相同名称的多个父组

[英]XSLT - Multiple parent groups of same name

Havent found an exact solution to this, for this basic example file I want to print the data of only the first 'Collection' (ie AH) and not the second, how do I go about that? Havent找到了一个精确的解决方案,对于这个基本示例文件,我只想打印第一个“集合”(即AH)而不是第二个“集合”的数据,我该如何处理? Thanks 谢谢

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

<Library>
    <Collection>
        <CD>
            <Title>A</Title>
            <Artist>B</Artist>
        </CD>
        <CD>
            <Title>C</Title>
            <Artist>D</Artist>
        </CD>
        <CD>
            <Title>E</Title>
            <Artist>F</Artist>
        </CD>
        <CD>
            <Title>G</Title>
            <Artist>H</Artist>
        </CD>
    </Collection>
    <Collection>
        <CD>
            <Title>I</Title>
            <Artist>J</Artist>
        </CD>
        <CD>
            <Title>K</Title>
            <Artist>L</Artist>
        </CD>
    </Collection>
</Library>

This will place the contents of the first Collection inside an HTML table, and will ignore the contents of the other collections: 这会将第一个Collection的内容放在HTML表中,并将忽略其他collection的内容:

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

    <xsl:output indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="Library">
        <table>
            <tr><th>Title</th><th>Artist</th></tr>
            <xsl:apply-templates/>
        </table>

    </xsl:template>

    <!-- Ignoring all collections -->
    <xsl:template match="Collection" />

    <!-- Not ignoring the first collection -->
    <xsl:template match="Collection[1]">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="CD">
        <tr>
            <td><xsl:value-of select="Title" /></td>
            <td><xsl:value-of select="Artist" /></td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

The following stylesheet: 以下样式表:

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

    <xsl:template match="/Library">
        <table>
            <tbody>
                <tr>
                    <td>Title</td>
                    <td>Artist</td>
                </tr>
                <xsl:apply-templates select="Collection[position() = 1]"/>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template match="Library/Collection">
        <xsl:for-each select="CD">
            <tr>
                <td><xsl:value-of select="Title"/></td>
                <td><xsl:value-of select="Artist"/></td>
            </tr>
        </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>

when applied to your input XML, produces: 应用于输入XML时,将产生:

<?xml version="1.0" encoding="utf-8"?>
<table>
   <tbody>
      <tr>
         <td>Title</td>
         <td>Artist</td>
      </tr>
      <tr>
         <td>A</td>
         <td>B</td>
      </tr>
      <tr>
         <td>C</td>
         <td>D</td>
      </tr>
      <tr>
         <td>E</td>
         <td>F</td>
      </tr>
      <tr>
         <td>G</td>
         <td>H</td>
      </tr>
   </tbody>
</table>

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

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