我有以下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>
我得到的问题很好,但我的答案有问题。我尝试过跟随,前面,而不是每个组,......有很多类似的问题,但是没有这样的格式退出,因为我的html文件中没有真正的子父结构。
答案 0 :(得分:3)
以这种方式尝试:
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>