xslt 展平 DocBook para 元素中的子元素 [英] xslt flattening out child elements in a DocBook para element

查看:21
本文介绍了xslt 展平 DocBook para 元素中的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些生成的 DocBook xml(来自 Doxygen)转换为我公司的 xml,它实际上是 DocBook 的一个子集.有一个像下面这样的 para 元素:

I am transforming some generated DocBook xml (from Doxygen) to my companies xml which is really a subset of DocBook. There is a para element like the following:

<para>some text.....
   <literallayout>
   </literallayout>
 more text....
 <table>
   ...
 </table>
 even more text
<table>...</table>
<literallayout>text also look here</literlayout>
more text <link xlink:href="http://someurl.com">
</para>

由于我们的 docbook 子集不喜欢 para 中的块元素,例如表格或图形,我想解析这个元素,并在这些文本周围放置新的 para 元素,这样我就会有这样的东西:

As our subset of docbook does NOT like block elements within a para, like table, or figure, i would like to parse this element, and put new para elements around those pieces of text, so that i would have something like this:

<para>some text.....
</para>
   <literallayout>
   </literallayout>
 <para>
  more text....
 </para>
 <table>
   ...
 </table>
 <para>
 even more text
 </para>
<table>...</table>
<literallayout>text also look here </literlayout>
 <para> more text</para>
 <para> <link xlink:href="http://someurl.com"></para>

以前,我以为我永远不会看到这么复杂的东西,所以我将表格放在像这样的 para 元素之外:

Previously, thinking i would never see anything this complex, i was putting tables outside of a para element like this:

<xsl:when test="( child::figure | child::table ) and (./text())">
    <Para>
        <xsl:value-of select="./text()"/>
    </Para>
    <xsl:apply-templates select="*"/>
</xsl:when>

但这最终只捕获了第一个文本节点,并把其他事情搞砸了.

But that ended up only catching the first text node, and messing other things up.

如果 para 元素如此混乱,任何人都可以建议,希望有一种优雅的方式来处理这个问题吗?

Can anyone suggest, hopefully an elegant way to handle this, if para elements are this messy?

谢谢,

罗斯

更新:我忽略了介绍一个角落案例.我已经编辑了上面的原始来源检查链接元素.当前解决方案从源中删除包含的 para 元素.

Update: I neglected to introduce a corner case. I've edited the original source above check the link element. The current solution removes the containing para element from the source.

推荐答案

我不得不更正您的 XML 示例中的一些内容,以使其格式正确.但以下:

I had to correct a bit of your XML example so that it was well-formed. But the following:

    <xsl:template match="para">
        <xsl:for-each select="node()">
            <xsl:choose>
                <xsl:when test="self::text() and normalize-space(.)!=''">
                    <xsl:element name="para">
                        <xsl:apply-templates select="."/>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="literallayout">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="table">
        <xsl:copy-of select="."/>
    </xsl:template>

输出:

<para>some text..... </para>
<literallayout>
</literallayout>
<para> more text.... </para>
<table> ... </table>
<para> even more text </para>
<table>...</table>
<literallayout>text also look here <link xlink:href="http://someurl.com"/></literallayout>
<para> more text. </para>

希望能帮到你.

这篇关于xslt 展平 DocBook para 元素中的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆