使用XSL提取元素内容

时间:2015-02-12 15:23:47

标签: xml xslt

我有一个简单的XML文件,我需要使用它来生成使用XSL的文档。

XML文件的基础是:

<badges>
   <badge id="s1">
   <name>Climbing</name>
   <description>This badge is awarded to scouts who have shown a proficiency in climbing</description>
      <levels>
         <availability level="beaver">Not Available</availability>
         <availability level="cub">Available</availability>
         <availability level="scout">Available</availability>
      </levels>
  </badge>
</badges>

我试图制作一份文件,向不同级别的侦察员显示活动的可用性。

例如,我想要一些看起来像的东西:

Climbing..
Beaver: Not available
Cub: Available
Scout: Available

我创建的XSL如下所示,问题在于迭代可用性元素。

<xsl:for-each select="badges/badge">
                    <!-- Sort ascending by name and print them out to the screen -->
                    <xsl:sort select="name" data-type="text" order="ascending"/>
                    <!-- check if it's an activity badge :) -->
                    <!-- Test value of badge, if activity then print to screen -->
                    <xsl:if test="@type='activity'">
                        <tr>
                            <td style="color: #3b5598; font-family:'Open Sans',sans-serif;">
                                <xsl:value-of select="name"/>
                                <br/>
                                <xsl:value-of select="description"/>
                            </td>
                        </tr>
                        <xsl:for-each select="badges/badge/levels">
                            <!-- Test value of badge, if activity then print to screen -->
                            <xsl:for-each select="availability">

                                <!-- Check the attirbute and then check the content of the element 

                                <xsl:if test="@level='beaver'">

                                    ho

                                </xsl:if>

                                <xsl:if test="@level='scout'">

                                    ho

                                </xsl:if>

                                <xsl:if test="@level='cub'">

                                    ho

                                </xsl:if>-->

                                Hi

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

如果有人有任何想法或建议,我很乐意听到他们的声音!

由于

1 个答案:

答案 0 :(得分:0)

您想要实现的确切输出不太清楚。您可以尝试以下方式:

<xsl:template match="/">
    <table border="1">
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>Beaver</th>
        <th>Cub</th>
        <th>Scout</th>
    </tr>
        <xsl:for-each select="badges/badge">
            <tr>
                <td><xsl:value-of select="name"/></td>
                <td><xsl:value-of select="description"/></td>
                <td><xsl:value-of select="levels/availability[@level='beaver']"/></td>
                <td><xsl:value-of select="levels/availability[@level='cub']"/></td>
                <td><xsl:value-of select="levels/availability[@level='scout']"/></td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

在你的例子中会产生:

enter image description here

或:

<xsl:template match="/">
    <table border="1">
        <xsl:for-each select="badges/badge">
            <tr><th colspan="2"><xsl:value-of select="name"/></th></tr>
            <tr><td colspan="2"><xsl:value-of select="description"/></td></tr>
            <xsl:for-each select="levels/availability">
                <tr>
                    <td><xsl:value-of select="@level"/></td>
                    <td><xsl:value-of select="."/></td>
                </tr>
            </xsl:for-each>
        </xsl:for-each>
    </table>
</xsl:template>

生产:

enter image description here