【问题标题】:XSL For-Each LoopingXSL For-Each 循环
【发布时间】:2012-10-11 04:45:12
【问题描述】:

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

<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:

<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 输出:

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 输出的样子:

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.

我已经为此花费了数小时,所以任何帮助都会非常棒!

【问题讨论】:

    标签: xml xslt loops xpath foreach


    【解决方案1】:

    在您的 XSLT 中,您尝试使用 xsl:for-each 迭代 PERSONAE,但您的示例 XML 不包含 PERSONAE 元素。除非您没有显示 XSLT 的更多内容,否则您不应该有除 <html> <body></body> </html> 之外的任何输出@

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

    <?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>
    

    【讨论】:

    • 您先生,真是个天才。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多