2017-09-06 1 views
-1

두 노드에서 노드 HTML 단락을 분할하고 다음과 같이 두 개의 노드로 분할하려는 : XML 위XSLT 내가 XML 노드 다음 한 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이며, 제 3 자에 의해 제공되기 때문에 변경할 수 없습니다. 이제 모든 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() 

이것은 아래에서 찾을 의미 root-> story-> first p "------- Complete story -----"를 포함하는 텍스트 노드를 만들고 그 레벨에서 그것을 수행하는 모든 노드를 선택하십시오. 마크 업을 포함한 완벽한 헤드 라인이 될 것입니다.

스포일러 경고 : 전체 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>