2017-09-06 76 views
-1

我有以下XML節點分割節點HTML段落,並希望將其拆分爲兩個節點如下:XSLT在兩個節點與HTML

<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可以轉換它。

+1

你到目前爲止嘗試過什麼? – zx485

回答

0

可以使用-------完整的故事-----標記作爲分隔符和選擇所有的也不是處理或之後它:

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

這意味着找到下根 - >故事 - >首先p包含「-------完整故事-----」的文本節點,然後選擇該級別上的所有節點。這將是包括標記在內的完整標題。

尾翼警報:完整的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> 
0

您可以使用節點()作爲模板來處理這個問題:

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>