gpt4 book ai didi

xslt - 使用 Xpath 选择每个节点和兄弟节点(直到该节点下一次出现)组合

转载 作者:行者123 更新时间:2023-12-02 11:49:20 24 4
gpt4 key购买 nike

我有以下 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>

关于xslt - 使用 Xpath 选择每个节点和兄弟节点(直到该节点下一次出现)组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40767321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com