xml – XSLT:选择以下兄弟节点,直到达到指定的标签
发布时间:2020-12-16 07:55:52 所属栏目:百科 来源:网络整理
导读:我正在尝试编写XSLT,它将在所选的以下兄弟节点上运行一个,但在达到另一个标签(h1)时停止. 以下是源XML: ?xml version="1.0"?html h1Test/h1 pTest: p 1/p pTest: p 2/p h1Test 2/h1 pTest2: p 1/p pTest2: p 2/p pTest2: p 3/p/html 这是XSLT: ?xml versio
我正在尝试编写XSLT,它将在所选的以下兄弟节点上运行一个,但在达到另一个标签(h1)时停止.
以下是源XML: <?xml version="1.0"?> <html> <h1>Test</h1> <p>Test: p 1</p> <p>Test: p 2</p> <h1>Test 2</h1> <p>Test2: p 1</p> <p>Test2: p 2</p> <p>Test2: p 3</p> </html> 这是XSLT: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <content> <xsl:apply-templates/> </content> </xsl:template> <xsl:template match="h1"> <section> <sectionHeading> <xsl:apply-templates/> </sectionHeading> <sectionContent> <xsl:for-each select="following-sibling::p"> <paragraph> <xsl:value-of select="."/> </paragraph> </xsl:for-each> </sectionContent> </section> </xsl:template> <xsl:template match="p"/> </xsl:stylesheet> 以下是当前结果: <?xml version="1.0" encoding="UTF-8"?> <content> <section> <sectionHeading>Test</sectionHeading> <sectionContent> <paragraph>Test: p 1</paragraph> <paragraph>Test: p 2</paragraph> <paragraph>Test: p 3</paragraph> <paragraph>Test2: p 1</paragraph> <paragraph>Test2: p 2</paragraph> </sectionContent> </section> <section> <sectionHeading>Test 2</sectionHeading> <sectionContent> <paragraph>Test2: p 1</paragraph> <paragraph>Test2: p 2</paragraph> </sectionContent> </section> </content> 这是预期的结果: <?xml version="1.0" encoding="UTF-8"?> <content> <section> <sectionHeading>Test</sectionHeading> <sectionContent> <paragraph>Test: p 1</paragraph> <paragraph>Test: p 2</paragraph> <paragraph>Test: p 3</paragraph> </sectionContent> </section> <section> <sectionHeading>Test 2</sectionHeading> <sectionContent> <paragraph>Test2: p 1</paragraph> <paragraph>Test2: p 2</paragraph> </sectionContent> </section> </content>
尝试这样:(而不是要求所有的p,我们要求最近之前h1是最新的p的所有p.)
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <content> <xsl:apply-templates/> </content> </xsl:template> <xsl:template match="h1"> <xsl:variable name="header" select="."/> <section> <sectionHeading> <xsl:apply-templates/> </sectionHeading> <sectionContent> <xsl:for-each select="following-sibling::p[preceding-sibling::h1[1] = $header]"> <paragraph> <xsl:value-of select="."/> </paragraph> </xsl:for-each> </sectionContent> </section> </xsl:template> <xsl:template match="p"/> </xsl:stylesheet> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |