XSLT 1.0 计数以下兄弟姐妹
XSLT 1.0 count following-siblings
我有一个简单的xml像这样
<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>
需要将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]
的文本。真不明白怎么算继兄妹。
这是我的尝试,递归处理 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>
我有一个简单的xml像这样
<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>
需要将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]
的文本。真不明白怎么算继兄妹。
这是我的尝试,递归处理 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>