2017-02-27 9 views
0

XSL을 사용하여 주제 요소를 정렬하고 싶습니다. 나는 많은 부분을 가지고 있는데, 나는 모든 부분을 분리 된 주제로 다루었 다. 지금은 다른 입력을받을 원하는, 그래서 내가주제 정렬을 변경해야합니다.

내 입력 XML이 아래와 같이 변경하려면 : 나는대로 받고 있어요

<xsl:template match="Segments"> 
<xsl:for-each-group select="*" group-starting-with="Segments"> 
<topic outputclass=""> 
<xsl:attribute name="id">topic_Head_<xsl:number count="Segments | Segment"/></xsl:attribute> 
<title/> 

<xsl:for-each-group select="current-group()" group-starting-with="Segment"> 
<xsl:choose> 
<xsl:when test="self::Segment"> 
<topic> 
<xsl:attribute name="id">topic_<xsl:number count="Segment"/></xsl:attribute> 
<xsl:apply-templates select="node()"/> 
</topic> 
</xsl:when> 
<xsl:otherwise> 
<body><xsl:apply-templates select="current-group()"/></body> 
</xsl:otherwise> 
</xsl:choose> 
</xsl:for-each-group> 
</topic> 
</xsl:for-each-group> 
</xsl:template> 

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

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

<xsl:template match="h3"> 
</xsl:template>   

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

출력 : 내가 사용

<Segments> 
<Segment> 
<Name>Food poisoning?</Name> 
<Body> 
<p>Food</p> 
</Body> 
</Segment> 
<Segment> 
<Name>Causes food poisoning?</Name> 
<Body> 
<p>Most</p> 
<h3>Salmonella</h3> 
<p>most</p> 
</Body> 
</Segment> 
</Segments> 

XSL을 :

<topic outputclass="" id="topic_Head_1"> 
<title/> 
<topic id="topic_1"> 
<title>Food poisoning?</title> 
<body> 
<p>Food</p> 
</body> 
</topic> 
<topic id="topic_2"> 
<title>Causes food poisoning?</title> 
<body> 
<p>Most</p> 

<p>most</p> 
</body> 
</topic> 
</topic> 

예상 출력과 같은 수 :

<topic outputclass="" id="topic_Head_1"> 
<title/> 
<topic id="topic_1"> 
<title>Food poisoning?</title> 
<body> 
<p>Food</p> 
</body> 
</topic> 
<topic id="topic_2"> 
<title>Causes food poisoning?</title> 
<body> 
<p>Most</p> 
</body> 
<topic id="topic_3"> 
<title>Salmonella</title> 
<body> 
<p>most</p> 
</body> 
</topic> 
</topic> 
</topic> 

I h3 태그가 필요한 것은 세그먼트 내의 다른 하위 주제와 같아야합니다. 제안을하십시오. 미리 감사드립니다

답변

1

내 생각은 : Body 요소는 h3 발생 여부와 상관없이 2 개의 다른 경로를 대상으로합니다. h3 경로는 간단하지 않습니다. H3 경로에 대한 :

  • 먼저 처리 첫째 H3 전에 모든 요소
  • 그룹의 모든 다른 요소, H3로 시작, eatch 그룹에 대한 주제 태그를 생성 및 템플릿
  • 여기

을 적용 코드

입니다
<xsl:template match="Body"> 
<xsl:choose> 
    <xsl:when test="h3"> 
     <body> 
     <xsl:apply-templates select="h3[1]/preceding-sibling::*"/> 
     </body>      
     <xsl:for-each-group select="h3[1] | h3[1]/following-sibling::*" group-starting-with="h3"> 
     <topic> 
      <xsl:attribute name="id">topic_<xsl:number count="Segment | h3" level="any"/></xsl:attribute> 
      <title><xsl:value-of select="." /></title> 
      <xsl:apply-templates select="current-group()"/> 
     </topic> 
     </xsl:for-each-group>  
    </xsl:when> 
    <xsl:otherwise> 
     <body> 
     <xsl:apply-templates select="node()"/> 
     </body> 
    </xsl:otherwise> 
</xsl:choose> 
</xsl:template> 
+0

잘 작동하는 @Lesiak. 그러나 첫 번째 h3 제목은 과 같은 para 콘텐츠가 가장 많습니다.입니다. 남은 h3 틸트 이외의 것은 잘 작동합니다. – User501

+0

XSL 변환 링크를 확인하십시오. http://xsltransform.net/94AbWAY/1 – User501

+0

"h3 [1] | h3 [1]/following-sibling :: *"(h3 [1]을 (를) 추가 한) 답변이 업데이트되었습니다. 이전 답변 나는 형제들 앞뒤로 만 처리했고 h3 elem을 생략했습니다. 당신은 이것을 간단하게 생각할 수 있습니다 : * [not (h3 [1]/following-sibling :: *)] – Lesiak