使用XSL用顺序替换子节点?

时间:2013-07-03 15:49:40

标签: xml xslt

我们收到的文件错误地生成如下:

<html>
    <body>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
            <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
                <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
            </p>
        </p>
    </body>
</html>

<p>个元素已嵌入到先前的<p>个节点中。它看起来应该是这样的:

<html>
    <body>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
        <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </body>
</html>

我们无法控制向我们发送文档的应用程序。我们如何使用XSL转换此文档,以便只将子节点(及其内容)呈现为兄弟节点?

2 个答案:

答案 0 :(得分:2)

如果以这种方式生成恶意的唯一元素是p,则您需要为p编写一个模板,首先调用所有属性和非p子元素的apply-templates,然后将模板应用于嵌入的p元素。在XSLT 2.0语法中:

<xsl:template match="p">
  <p><xsl:apply-templates select="node() except p"/></p>
  <xsl:apply-templates select="p"/>
</xsl:template>

样式表的其余部分需要执行身份转换。

如果其他元素在输入中也不适当地自我嵌套,则需要同样处理它们。

如果您使用的是XSLT 1.0而不是2.0,那么您需要找到一些其他方法来区分属于p内部的内容以及之后应该发生的内容,因为node() except p在XSLT中不合法1.0选择值。我自己使用模式:

<xsl:template match="p">
  <p><xsl:apply-templates mode="para-contents"/></p>
  <xsl:apply-templates select="p"/>
</xsl:template>

<xsl:template match="node()" mode="para-contents">
  <xsl:apply-templates select="."/>
</xsl:template>
<xsl:template match="p" mode="para-contents"/>

或(正如Ian Roberts在评论中建议的那样)只需将node() except p替换为node()[not(self::p)]

这假设除了p之外的某些元素可能出现在输入的body元素中;如果只有p发生,Nils Werner提供的解决方案将会很好。

然而,在现实生活中,如果我必须处理这样的输入,我可能会对它进行整理,而不是使用我自己的XSLT样式表来完成Tidy所做的一小部分。

答案 1 :(得分:0)

您可以尝试以下操作:

<?xml version='1.0'?>
<xsl:stylesheet
    version='1.0'
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method="xml" 
    indent="yes" />

<xsl:template match="/">
    <xsl:apply-templates select="html/body/*" mode="fixnested" />
</xsl:template>

<xsl:template match="*" mode="fixnested">
    <xsl:element name="{name()}">
        <xsl:apply-templates select="@* | text()" mode="fixnested" />
    </xsl:element>
    <xsl:apply-templates select="*" mode="fixnested" />
</xsl:template>

<xsl:template match="@*" mode="fixnested">
    <xsl:attribute name="{name(.)}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

正如你所看到的那样,我把它保持得非常抽象,所以你可以向它提供任何XML(不仅仅是嵌套的<p>)并让它变得扁平化。这些模板保留了属性和内容。

相关问题