【发布时间】:2013-02-27 07:05:14
【问题描述】:
我有以下平面 XML 结构
<div class="section-level-1">
<!-- other elements -->
<p class="para">
<img src="..." alt="..." title="..." />
</p>
<p class="figure-caption-german">
<img src="..." alt="..." title="..." />
</p>
<p class="figure-caption-english">
<img src="..." alt="..." title="..." />
</p>
<!-- other elements -->
<p class="para">
<img src="..." alt="..." title="..." />
</p>
<p class="figure-caption-german">
<img src="..." alt="..." title="..." />
</p>
<misc-element>...</misc-element>
<p class="figure-caption-english">
<img src="..." alt="..." title="..." />
</p>
</div>
这些元素的顺序总是相同的(para -> figure-caption-german -> figure-caption-english),但是我不能排除它会被其他元素打断(这里是 misc-元素)。
我想将这三个元素包装在一个元素中
<div class="section-level-1">
<!-- other elements -->
<div class="figure">
<p class="para">
<img src="..." alt="..." title="..." />
</p>
<p class="figure-caption-german">
<img src="..." alt="..." title="..." />
</p>
<p class="figure-caption-english">
<img src="..." alt="..." title="..." />
</p>
</div>
<!-- other elements -->
<div class="figure">
<p class="para">
<img src="..." alt="..." title="..." />
</p>
<p class="figure-caption-german">
<img src="..." alt="..." title="..." />
</p>
<p class="figure-caption-english">
<img src="..." alt="..." title="..." />
</p>
</div>
</div>
中断元素不需要保留,可以删除。
我目前所拥有的
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<!-- Html Ninja Pattern -->
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="* | @* | text()"/>
</xsl:element>
</xsl:template>
<xsl:template match="body//@*">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<!-- Modify certain elements -->
<xsl:template match="" priority="1">
<!-- do something -->
</xsl:template>
作为一种基本模式,我使用了“Html Ninja 技术”(http://getsymphony.com/learn/articles/view/html-ninja-technique/),因为它允许我只处理那些我需要转换的特定元素,同时将所有其他元素原封不动地发送到输出树。 到目前为止一切正常,但现在我真的似乎遇到了障碍。我什至不确定是否可以依靠“Html Ninja Technique”来完成所需的任务。
任何帮助或指示将不胜感激。
最好的问候,谢谢你,马蒂亚斯·艾因布罗德
【问题讨论】:
标签: xslt xslt-2.0 xslt-grouping