2015-02-06 3 views
0

나는이XSLT 1.0 수가 다음과 같은 형제 자매

<root> 
      <story> 
      <strongp> 
       <color>Attention</color> 
       Text of the single strongp color. 
      </strongp> 
      <p>Text</p> 
      <strongp> 
       <color>Attention</color> 
       Text of strongp color. 
      </strongp> 
      <strongp> 
       Text of interest1 
       <a id="1234-3457">here</a>. 
      </strongp> 
      <p>sometext</p> 
      <p>sometext</p> 
      <el>sometext</el> 
      <h5>Header H5</h5> 
      <strongp> 
       <color>Attention</color> 
       Text of strongp color. 
      </strongp> 
      <strongp> 
       Text of interest 
       <a id="8909-3490">here</a> 
      </strongp> 
      <strongp> 
       Text of interest 
       <a id="8909-0081">here</a> 
      </strongp> 
      <strongp> 
       Text of interest 
       <a id="8967-001">here</a> 
      </strongp> 
      <p>Text</p> 

      <inline /> 
     </story> 
    </root> 

같은 간단한 XML은 <remark>이 모든 remarktextremarkheader 다음과 같은 형제 자매로 strongp[color] 변환 및 포장이 필요합니다.

색상 단일 <strongp> 필요한 출력 :

<remark> 
    <remarkheader>Attention</remarkheader> 
    <remarktext>Text of the single strongp color.</remarktext> 
</remark> 

1 형제 strongp 다음 strongp with color의 출력은 :

<remark> 
    <remarkheader>Attention</remarkheader> 
    <remarktext>Text of strongp color.<br/>Text of interest1 
       <a id="1234-3457">here</a>.</remarktext> 
</remark> 

이상 1 strongp 하였다 strongp color 대한 마지막 출력

<remark> 
    <remarkheader>Attention</remarkheader> 
    <remarktext> 
     Text of strongp color. 
     <br/> 
     Text of interest1 
     <a id="1234-3457">here</a>. 
    <br/> 
     Text of interest 
     <a id="8909-3490">here</a>. 
    <br/> 
     Text of interest 
     <a id="8909-0081">here</a>. 
    <br/> 
     Text of interest 
     <a id="8967-001">here</a>. 
    </remarktext> 
</remark> 

음,이 템플릿

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="node()|@*" name="identity" > 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="strongp[color]"> 
     <remark> 
      <remarkheader> 
       <xsl:value-of select="./color"/> 
      </remarkheader> 
      <remarktext> 
       <xsl:value-of select="substring-after(., color)"/> 
       <br/> 
       <xsl:for-each select="following-sibling::strongp[count(preceding-sibling::strongp[color][1] | current())=1]"> 
        <xsl:apply-templates select="./node()" /> 
        <xsl:choose> 
         <xsl:when test="position() !=last()"> 
          <br/> 
         </xsl:when> 
        </xsl:choose> 
       </xsl:for-each> 
      </remarktext> 
     </remark> 
    </xsl:template> 

</xsl:stylesheet> 

을 적용하지만 잘 작동하지 않는 것 같습니다. 첫 번째 다음 형제 인 strongp[color]의 텍스트를 추가합니다. 나는 그것이 다음 형제 수를 어떻게 계산하는지 정말로 이해하지 못한다.

당신은 반복적으로 여기 http://www.utilities-online.info/xsltransformation/?save=c1b3e857-b6d7-4562-a440-9d3e357cb12d-xsltransformation

답변

1

내 시도는, 여기에서 처리 following-sibling::strongp 볼 수 있습니다 :

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

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="strongp[color]"> 
    <remark> 
     <remarkheader> 
      <xsl:value-of select="color"/> 
     </remarkheader> 
     <remarktext> 
      <xsl:apply-templates select="text()"/> 
      <xsl:apply-templates select="following-sibling::*[1][self::strongp]" mode="following"/> 
     </remarktext> 
    </remark> 
</xsl:template> 

<xsl:template match="strongp" mode="following"> 
    <br/> 
    <xsl:apply-templates select="node()"/> 
    <xsl:apply-templates select="following-sibling::*[1][self::strongp]" mode="following"/> 
</xsl:template> 

<xsl:template match="strongp"/> 

</xsl:stylesheet> 
+0

감사합니다 많이! 그것은 작동합니다. 올바른 것으로 승인되었습니다. – sprut