gpt4 book ai didi

xml - XSL每次循环

转载 作者:行者123 更新时间:2023-12-03 16:06:28 24 4
gpt4 key购买 nike

基本上,这是我的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.


我已经花了几个小时,所以任何帮助都会很棒!

最佳答案

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

与其使用xsl:for-each,还可以通过使用“推”样式(使用xsl:apply-templates和特定模板来匹配并生成期望的输出)来更简单地实现期望的输出。

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

关于xml - XSL每次循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12993393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com