2

基本的に、これはシェイクスピア劇の抜粋から取った私の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.

私はこれに何時間も費やしたので、どんな助けもとても素晴らしいでしょう!

4

1 に答える 1

4

In your XSLT you are attempting to use xsl:for-each to iterate over PERSONAE, but your sample XML does not contain a PERSONAE element. Unless there is more to your XSLT that you are not showing, you should not have any output other than <html> <body></body> </html>

Rather than using xsl:for-each, you could achieve the desired output more simply by using a "push" style that uses xsl:apply-templates and specific templates to match and generate desired output.

<?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>
于 2012-10-20T23:41:25.913 に答える