XSLT用html将节点html段拆分为两个节点

时间:2017-09-06 12:24:45

标签: xml xslt

我有以下xml节点,并希望将其拆分为两个节点,如下所示:

<root>
    <story>
        <p>Headlines:<br/>1- First news headline<br/>2- Second news headline<br/>
        <br/>
        <br/>-------Complete story-----<br/>1- First news headline story<br/>
        <br/>Some detailed news story will apprear related to first headline<br/>
        <br/>
        <br/>2- Second news headline story<br/>
        <br/>Some details about second news story will be inserted here<br/>
        </p>
    </story>
</root>

以上XML是我的输入xml,我无法更改它,因为它是由某些第三方提供的。现在我想将它分成两个节点,保留所有的html标记。输出xml应如下所示:

<root>
<headlines>
<p>Headlines:<br/>1- First news headline<br/>2- Second news headline<br/>
<br/>
<br/>-------</p>
</headlines>
<stories>
<p>Complete story-----<br/>1- First news headline story<br/>
<br/>Some detailed news story will apprear related to first headline<br/>
<br/>
<br/>2- Second news headline story<br/>
<br/>Some details about second news story will be inserted here<br/>
</p>
</stories>
</root>

您可能会观察到原始<p></p>标记被分为两段。请帮助正确的xslt,它可以转换它。

2 个答案:

答案 0 :(得分:0)

您可以使用-------完整的故事-----标记作为分隔符,并选择所有不处理或跟随它:

/root/story/p[1]/text()[. = '-------Complete story-----']/preceding::node()

这意味着在root-&gt; story-&gt;下找到包含“-------完整故事-----”的文本节点,然后选择该级别上的所有节点进行处理。这将是包括标记在内的完整标题。

SPOILER ALERT:完整的XSLT如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:fn="http://www.w3.org/2005/xpath-functions">

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

    <xsl:template match="/">

        <root>
            <headlines>
                <p>
                    <xsl:copy-of name="headlines" select="root/story/p[1]/text()[. = '-------Complete story-----']/preceding::node()" />
                    <br/>
                    <br/>-------</p>
            </headlines>
            <stories>
                <p>
                    <xsl:copy-of name="headlines" select="root/story/p[1]/text()[. = '-------Complete story-----']/following::node()" />
                </p>
            </stories>
        </root>
    </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

您可以使用node()作为模板来处理:

<强> XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <root>
            <xsl:apply-templates/>
        </root>
    </xsl:template>

    <xsl:template match="node()[matches(., '^Headlines:$')]" priority="100">
        <headlines>
            <p>
                <xsl:copy-of select=".|following::node() except (following::node()[preceding::node()[matches(., '^-------Complete story-----$')] or matches(., '^-------Complete story-----$')])"/>
            </p>
        </headlines>
    </xsl:template>

    <xsl:template match="node()[matches(., '^-------Complete story-----$')]">
        <stories>
            <p>
                <xsl:copy-of select=".|following::node()"/>
            </p>
        </stories>
    </xsl:template>

    <xsl:template match="p/node()[not(matches(., '^Headlines:$|^-------Complete story-----$'))]"></xsl:template>

</xsl:stylesheet>

<强>输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <headlines>
      <p>Headlines:<br/>1- First news headline<br/>2- Second news headline<br/>
         <br/>
         <br/>
      </p>
   </headlines>
   <stories>
      <p>-------Complete story-----<br/>1- First news headline story<br/>
         <br/>Some detailed news story will apprear related to first headline<br/>
         <br/>
         <br/>2- Second news headline story<br/>
         <br/>Some details about second news story will be inserted here<br/>
      </p>
   </stories>
</root>