首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XSL For-Each循环

XSL For-Each循环
EN

Stack Overflow用户
提问于 2012-10-21 06:52:35
回答 1查看 9.7K关注 0票数 2

基本上,这是我的XML摘录自莎士比亚戏剧:

代码语言:javascript
运行
复制
<PLAY>
<PERSONA>BENEDICK, a young lord of Padua.</PERSONA>
<PERSONA>LEONATO, governor of Messina.</PERSONA>
<PERSONA>ANTONIO, his brother.</PERSONA>
<PERSONA>BALTHASAR, attendant on Don Pedro.</PERSONA>
<PGROUP>
    <PERSONA>CONRADE</PERSONA>
    <PERSONA>BORACHIO</PERSONA>
    <GRPDESCR>followers of Don John.</GRPDESCR>
</PGROUP>
<PERSONA>FRIAR FRANCIS</PERSONA>
</PLAY>

下面是XSL:

代码语言:javascript
运行
复制
<xsl:template match="PLAY">
    <html>
    <body>
         <xsl:for-each select="PERSONAE">

              <xsl:apply-templates select="PERSONA" />
              <xsl:apply-templates select="PGROUP/PERSONA" />

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

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

<xsl:template match="PGROUP/PERSONA">
    <xsl:for-each select=".">
        <p><xsl:value-of select="." />, </p> 
    </xsl:for-each>

    <xsl:for-each select="..">
        <p><xsl:value-of select="GRPDESCR" /></p>
    </xsl:for-each>
</xsl:template>

当前HTML输出:

代码语言:javascript
运行
复制
BENEDICK, a young lord of Padua.

LEONATO, governor of Messina.

ANTONIO, his brother.

BALTHASAR, attendant on Don Pedro.

FRIAR FRANCIS

CONRADE,

followers of Don John.

BORACHIO,

followers of Don John.

这是我想要的HTML输出的样子:

代码语言:javascript
运行
复制
BENEDICK, a young lord of Padua.

LEONATO, governor of Messina.

ANTONIO, his brother.

BALTHASAR, attendant on Don Pedro.

FRIAR FRANCIS

CONRADE, BORACHIO, followers of Don John.

我已经在这上面花了几个小时了,所以任何帮助都会很好!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-21 07:41:26

在您的XSLT中,您试图使用xsl:for-each遍历PERSONAE,但是您的示例XML不包含PERSONAE元素。除非您的XSLT有更多未显示的内容,否则不应该有<html> <body></body> </html>以外的任何输出。

与使用xsl:for-each相比,您可以更简单地通过使用“推”样式来实现所需的输出,该样式使用xsl:apply-templates和特定的模板来匹配和生成所需的输出。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>

    <xsl:template match="PLAY">
        <html>
            <body>
                <!--first, process all PERSONA elements 
                    that are children of PLAY-->
                <xsl:apply-templates select="PERSONA" />
                <!--Then, process all PGROUP elements -->
                <xsl:apply-templates select="PGROUP" />
            </body>
        </html>
    </xsl:template>

    <!--generic template match for PERSONA elements -->
    <xsl:template match="PERSONA">
        <p><xsl:value-of select="." /></p>
    </xsl:template>

    <!--For each PGROUP matched, create a P and apply templates 
        to child elements(i.e. PERSONA and GRPDESCR) -->
    <xsl:template match="PGROUP">
        <p>
            <!--Note: No specific template is defined for GRPDESCR. 
               The default XSLT template rules will apply for GRPDESCR 
               and it will copy it's text to output -->
            <xsl:apply-templates select="*" />
        </p> 
    </xsl:template>

    <!--A more specific match for PERSONA elements that will select the text 
        and then add ", " -->
    <xsl:template match="PGROUP/PERSONA">
        <xsl:value-of select="."/>
        <xsl:text>, </xsl:text> 
    </xsl:template>

</xsl:stylesheet>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12993393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档