如何应用XSL模板从文档的不同部分开始和结束XML元素



我想弄清楚如何创建一个HTML文档,有时从XML源的一组元素被'包装'在一个引导面板。

XML描述了垂直的表单布局。你有一组包含单个字段的行。在有一个PANELSTART字段的地方,我想在屏幕上启动一个面板,继续写出内容,然后当我击中PANELEND字段时,我想用匹配的</div>关闭面板。但是XSL不允许这样做,因为所有元素都需要一个匹配的结束元素。
我试过了

<xsl:text>

但这似乎也不起作用。我有一个例子来说明。

源XML:

<?xml version="1.0"?>
<rows>
    <row><field type="B><plainValue>B0</plainValue></field></row>
    <row><field type="PANELSTART"></field></row>
    <row><field type="B"><plainValue>B1</plainValue></field></row>
    <row><field type="PANELEND"/></row>
    <row><field type="C"><plainValue>C0</plainValue></field></row>
    <row><field type="D"><plainValue>D0</plainValue></field></row>
    <row><field type="B"><plainValue>B2</plainValue></field></row>
</rows>
XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />
 <xsl:template match="field[@type='PANELSTART']">
    <div class='panel'>panel header</div>
        <div class='panel-body'>
 </xsl:template>
 <xsl:template match="field[@type='B']">
     <p>content goes here</p>
 </xsl:template>
 <xsl:template match="field[@type='PANELEND']">
     </div>
 </xsl:template>
</xsl:stylesheet>

预期结果:

    <p>B0</p>
    <div class="panel">panel header</div>
    <div class="panel-body">
       <p>B1</p>
    </div>
    <p>C0</p>
    <p>D0</p>
    <p>B2</p>    
我很感激你对如何做这件事的想法。不幸的是,我不能改变XML的结构。

在XSLT 1.0中可以使用"兄弟递归":

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes" version="5.0"/>
    <xsl:template match="rows">
        <div>
            <xsl:apply-templates select="row[1]"/>
        </div>
    </xsl:template>
    <xsl:template match="row[field[@type = 'PANELSTART']]">
        <div class="panel">panel header</div>
        <div class="panel-body">
            <xsl:apply-templates select="following-sibling::*[1]"/>
        </div>
        <xsl:apply-templates select="following-sibling::row[field[@type = 'PANELEND']][1]/following-sibling::*[1]"/>
    </xsl:template>
    <xsl:template match="row">
        <p>
            <xsl:apply-templates/>
        </p>
        <xsl:apply-templates select="following-sibling::*[1][not(self::row[field[@type = 'PANELEND']])]"/>
    </xsl:template>
</xsl:transform>

网址:http://xsltransform.net/bdxtqY/2

如果您能够使用XSLT 2.0,您可能会使用xsl:for-each-group及其group-starting-with属性

 <xsl:for-each-group select="row/field" group-starting-with="field[@type='PANELSTART']">

因此,每个带有type属性"PANELSTART"的字段构成每个组的开始。

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" encoding="UTF-8" indent="yes" />
    <xsl:template match="rows">
        <xsl:for-each-group select="row/field" group-starting-with="field[@type='PANELSTART']">
        <div class='panel'>panel header</div>
            <div class='panel-body'>
                <xsl:apply-templates select="current-group()" />                
            </div>
        </xsl:for-each-group>
    </xsl:template>
    <xsl:template match="field[@type='B']">
        <p>content goes here</p>
    </xsl:template>
    <xsl:template match="field[@type='PANELEND']" />
</xsl:stylesheet>

你也许可以删除匹配"PANELEND"的模板。

在http://www.xml.com/pub/a/2003/11/05/tr.html阅读xsl:for-each-group

此XSLT忽略所有PANELSTARTPANELEND行(实际上除B之外的所有行—但如果需要,可以重新组织为只忽略 PANELSTARTPANELEND)。它不是循环fields,而是循环rows。这意味着整个field/type B集被视为单个实体,因此您可以在其周围插入div

对于多次出现的rows,它将正确工作(每次都将获得自己的div),并且当其中没有"类型B"时,它将忽略整个fields集。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />
 <xsl:template match="rows">
    <xsl:if test="row/field/@type='B'">
    <div class='panel'>panel header</div>
    <div class='panel-body'>
        <xsl:apply-templates select="row/field[@type='B']" mode="process" />
    </div>
    </xsl:if>
</xsl:template>
<xsl:template match="field[@type='B']" mode="process">
    [<xsl:value-of select="." />]
</xsl:template>
</xsl:stylesheet>

最新更新