我有以下平面 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-element)打断。
我想将这三个元素包装在一个元素中
<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 Technique”(http://getsymphony.com/learn/articles/view/html-ninja-technique/),因为它允许我只处理那些我需要转换的特定元素,而将所有其他元素不变地发送到输出树。到目前为止,一切正常,但现在我似乎真的遇到了障碍。我什至不确定是否可以依靠“Html Ninja Technique”来完成所需的任务。
任何帮助或指示将不胜感激。
最好的问候,谢谢你,马蒂亚斯·艾因布罗德