2015-02-06 64 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需要转换成strongp[color]remarkheader以下的兄弟姐妹到remarktext和包装这一切<remark>

为单<strongp>与颜色所需的输出:

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

和输出用于strongp with color随后用1个同级strongp是:

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

最后输出strongp color接着超过1 strongp

<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 2015-02-06 12:59:52