在XSL 1.0中,我进行了搜索并找到了类似的分组项目,但我认为这略有不同.抱歉,如果已经涵盖了这一点,我一直无法找到答案 输入 ?xml version="1.0"?xmldocsection paragraphMarker="true"Part 1. /sectionse
输入
<?xml version="1.0"?> <xmldoc> <section paragraphMarker="true">Part 1. </section> <section paragraphMarker="false">Part 2. </section> <section paragraphMarker="false">Part 3. </section> <section paragraphMarker="true">Part 4. </section> <section paragraphMarker="true">Part 5. </section> <section paragraphMarker="false">Part 6. </section> </xmldoc>
期望的输出
<p>Part 1. Part 2. Part 3.</p> <p>Part 4. </p> <p>Part 5. Part 6. </p>
我尝试过以下方法: –
<xsl:key name="sectionsWithParagraphMarker" match="section[@paragraphMarker='true']" use="."/> <xsl:template match="/"> <xsl:for-each select= "/xmldoc/section[generate-id() = generate-id(key('sectionsWithParagraphMarker',.)[1])]"> <p> <xsl:apply-templates select="."/> <xsl:apply-templates select="./following-sibling::node() [count(. | /xmldoc/section[@paragraphMarker='true'][1]/ preceding-sibling::node()) = count(/xmldoc/section[@paragraphMarker='true'][1]/ preceding-sibling::node()) ]"/> </p> </xsl:for-each> </xsl:template> <xsl:template match="section"> <xsl:select value-of="."/> </xsl:template>
这不起作用,我一直坚持下去.它为所有组选择了太多“节”节点.感谢任何帮助!
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="section"> <xsl:if test="@paragraphMarker='true'"> <p> <xsl:apply-templates select="." mode="text" /> </p> </xsl:if> </xsl:template> <xsl:template match="section" mode="text"> <xsl:value-of select="." /> <xsl:apply-templates select="following-sibling::section[1][@paragraphMarker='false']" mode="text" /> </xsl:template> </xsl:stylesheet>
由于此代码仅向前看,因此它比执行回溯的解决方案更有效(在许多实现中向后的XPath轴很慢).