我有以下 html 结构:
<document>
<ol>a question</ol>
<div>answer</div>
<div>answer</div>
<ol>another question</ol>
<div>answer</div>
<ol>question #3</ol>
...
</document>
我想采取<ol>
节点和以下<div>
直到下一个节点<ol>
节点,所以我可以将它们分组在 xml 中,例如
<vce>
<topic>
<question> ... </question>
<answer> ... </answer>
</topic>
...
</vce>
到目前为止我有以下内容
<xsl:for-each select="//body/ol">
<document>
<content name="question">
<xsl:value-of select="." />
</content>
<content name="answer">
<xsl:for-each
select="./following-sibling::div !!! need code here !!!>
<xsl:value-of select="." />
</xsl:for-each>
</content>
</document>
</xsl:for-each>
我很好地回答了问题,但我很难回答。我尝试过使用以下,之前,不,for-each-group,...。有很多类似的问题,但不能像这样使用这种格式退出,因为我的 html 文件中实际上没有子父结构。
试试这样:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="answers" match="div" use="generate-id(preceding-sibling::ol[1])" />
<xsl:template match="/document">
<vce>
<xsl:for-each select="ol">
<topic>
<question>
<xsl:value-of select="." />
</question>
<xsl:for-each select="key('answers', generate-id())">
<answer>
<xsl:value-of select="." />
</answer>
</xsl:for-each>
</topic>
</xsl:for-each>
</vce>
</xsl:template>
</xsl:stylesheet>
当应用于以下测试输入时:
XML
<document>
<ol>question A</ol>
<div>answer A1</div>
<div>answer A2</div>
<ol>question B</ol>
<div>answer B1</div>
<ol>question C</ol>
<div>answer C1</div>
<div>answer C2</div>
</document>
结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<vce>
<topic>
<question>question A</question>
<answer>answer A1</answer>
<answer>answer A2</answer>
</topic>
<topic>
<question>question B</question>
<answer>answer B1</answer>
</topic>
<topic>
<question>question C</question>
<answer>answer C1</answer>
<answer>answer C2</answer>
</topic>
</vce>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)