我有一个XML文档,其中包含元素类型中的多个实例和子类型,并且需要按顺序显示它们,并为每种子类型使用不同的格式。
我尝试了for-each
和条件choose
循环的不同组合,但没有运气。
XML细分:
<PLAY>
<PERSONAE>
<TITLE>Dramatis Personae</TITLE>
<PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
<PERSONA>HAMLET, son to the late, and nephew to the present king.</PERSONA>
<PERSONA>POLONIUS, lord chamberlain. </PERSONA>
<PERSONA>HORATIO, friend to Hamlet.</PERSONA>
<PERSONA>LAERTES, son to Polonius.</PERSONA>
<PERSONA>LUCIANUS, nephew to the king.</PERSONA>
<PGROUP>
<PERSONA>VOLTIMAND</PERSONA>
<PERSONA>CORNELIUS</PERSONA>
<PERSONA>ROSENCRANTZ</PERSONA>
<PERSONA>GUILDENSTERN</PERSONA>
<PERSONA>OSRIC</PERSONA>
<GRPDESCR>courtiers.</GRPDESCR>
</PGROUP>
<PERSONA>A Gentleman</PERSONA>
<PERSONA>A Priest. </PERSONA>
<PGROUP>
<PERSONA>MARCELLUS</PERSONA>
<PERSONA>BERNARDO</PERSONA>
<GRPDESCR>officers.</GRPDESCR>
</PGROUP>
XSL:
<h2><xsl:value-of select="PLAY/PERSONAE/TITLE" /></h2>
<xsl:for-each select="PLAY/PERSONAE/node()">
<xsl:choose>
<xsl:when test="PERSONA">
<xsl:value-of select="PERSONA" />
</xsl:when>
<xsl:when test="PGROUP">
<xsl:for-each select="PERSONA">
<xsl:value-of select="PERSONA" />
</xsl:for-each>
<xsl:value-of select="PGROUP" />
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</xsl:for-each>
输出
Dramatis Personae
VOLTIMANDMARCELLUS
预期(有希望)输出
Dramatis Personae
CLAUDIUS, king of Denmark.
HAMLET, son to the late, and nephew to the present king.
POLONIUS, lord chamberlain.
HORATIO, friend to Hamlet.
LAERTES, son to Polonius.
LUCIANUS, nephew to the king.
VOLTIMAND
CORNELIUS
ROSENCRANTZ
GUILDENSTERN
OSRIC
A Gentleman
A Priest.
MARCELLUS
BERNARDO
FRANCISCO, a soldier.
REYNALDO, servant to Polonius.
Players.
Two Clowns, grave-diggers.
FORTINBRAS, prince of Norway.
A Captain.
English Ambassadors.
GERTRUDE, queen of Denmark, and mother to Hamlet.
OPHELIA, daughter to Polonius.
Lords, Ladies, Officers, Soldiers, Sailors, Messengers, and other Attendants.
哈姆雷特之父的幽灵。
每次,它只返回它解析的第一个PERSONA子节点,或者返回前两个PGROUP / PERSONA子节点。谁能看到我做错了什么?
答案 0 :(得分:1)
你可能想要更像的东西:
<xsl:template match="PERSONAE">
<xsl:value-of select="TITLE"/>
<xsl:apply-templates select="PERSONA|PGROUP"/>
</xsl:template>
<xsl:template match="PGROUP">
<xsl:apply-templates select="PERSONA"/>
</xsl:template>
<xsl:template match="PERSONA">
<xsl:value-of select="."/>
</xsl:template>
您:
<xsl:for-each select="PLAY/PERSONAE/node()">
只是选择每个PERSONAE的第一个节点子项
答案 1 :(得分:1)
如果我正确猜测,您需要HTML输出,而不是文本。在这里,您可以获得一种结果,在渲染时,它看起来就像您发布的内容:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/PLAY">
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:template match="PERSONAE">
<h2>
<xsl:value-of select="TITLE" />
</h2>
<div>
<xsl:apply-templates select="PERSONA | PGROUP"/>
</div>
</xsl:template>
<xsl:template match="PERSONA">
<xsl:value-of select="." />
<br/>
</xsl:template>
<xsl:template match="PGROUP">
<p>
<xsl:apply-templates select="PERSONA"/>
</p>
</xsl:template>
</xsl:stylesheet>
当应用于以下测试输入时:
<PLAY>
<PERSONAE>
<TITLE>Dramatis Personae</TITLE>
<PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
<PERSONA>HAMLET, son to the late, and nephew to the present king.</PERSONA>
<PERSONA>POLONIUS, lord chamberlain. </PERSONA>
<PERSONA>HORATIO, friend to Hamlet.</PERSONA>
<PERSONA>LAERTES, son to Polonius.</PERSONA>
<PERSONA>LUCIANUS, nephew to the king.</PERSONA>
<PGROUP>
<PERSONA>VOLTIMAND</PERSONA>
<PERSONA>CORNELIUS</PERSONA>
<PERSONA>ROSENCRANTZ</PERSONA>
<PERSONA>GUILDENSTERN</PERSONA>
<PERSONA>OSRIC</PERSONA>
<GRPDESCR>courtiers.</GRPDESCR>
</PGROUP>
<PERSONA>A Gentleman</PERSONA>
<PERSONA>A Priest. </PERSONA>
<PGROUP>
<PERSONA>MARCELLUS</PERSONA>
<PERSONA>BERNARDO</PERSONA>
<GRPDESCR>officers.</GRPDESCR>
</PGROUP>
</PERSONAE>
</PLAY>
结果将是:
<body>
<h2>Dramatis Personae</h2>
<p>CLAUDIUS, king of Denmark. <br>HAMLET, son to the late, and nephew to the present king.<br>POLONIUS, lord chamberlain. <br>HORATIO, friend to Hamlet.<br>LAERTES, son to Polonius.<br>LUCIANUS, nephew to the king.<br><p>VOLTIMAND<br>CORNELIUS<br>ROSENCRANTZ<br>GUILDENSTERN<br>OSRIC<br></p>A Gentleman<br>A Priest. <br><p>MARCELLUS<br>BERNARDO<br></p></p>
</body>
呈现为: