使用 Xpath 选择每个节点和兄弟节点(直到该节点下一次出现)组合 [英] Selecting every node and siblings (until next occurrence of that node) combinations with Xpath

查看:35
本文介绍了使用 Xpath 选择每个节点和兄弟节点(直到该节点下一次出现)组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 html 结构:

<ol>一个问题</ol><div>答案</div><div>答案</div><ol>另一个问题</ol><div>答案</div><ol>问题#3</ol>...</文档>

我想取

    节点和后面的

    节点,直到下一个

      节点,所以我可以将它们分组在一个 xml 中,如

      <话题><问题>... </问题><回答>... </answer></topic>...</vce>

      到目前为止,我有以下内容

      <文档><内容名称=问题"><xsl:value-of select="."/></内容><内容名称=答案"><xsl:for-eachselect="./following-sibling::div !!! 这里需要代码!!!><xsl:value-of select="."/></xsl:for-each></内容></文档></xsl:for-each>

      我能很好地回答问题,但我在回答问题时遇到了麻烦.我曾尝试使用以下、前置、不、for-each-group、... .有很多类似的问题,但不会像这样使用这种格式退出,因为我的 html 文件中并没有真正的父级结构.

      解决方案

      试试看:

      XSLT 1.0

      <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"><话题><问题><xsl:value-of select="."/></问题><xsl:for-each select="key('answers', generate-id())"><回答><xsl:value-of select="."/></回答></xsl:for-each></topic></xsl:for-each></vce></xsl:模板></xsl:stylesheet>

      应用于以下测试输入时:

      XML

      <ol>问题A</ol><div>答案 A1</div><div>答案 A2</div><ol>问题B</ol><div>回答 B1</div><ol>问题C</ol><div>回答 C1</div><div>回答 C2</div></文档>

      结果将是:

      I have the following html structure:

      <document>
      <ol>a question</ol>
      <div>answer</div>
      <div>answer</div>
      <ol>another question</ol>
      <div>answer</div>
      <ol>question #3</ol>
      ...
      </document>
      

      I would like to take the <ol> nodes and the following <div> nodes until the next <ol> node, so I can group them in an xml like

      <vce>
        <topic>
         <question> ... </question>
         <answer> ... </answer>
        </topic>
        ...
      </vce>
      

      So far I have the following

      <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>
      

      I get the questions just fine but I'm having trouble with the answers. I have tried working with following, preceding, not, for-each-group, ... . There are many similar questions but not quit like this with this format because I don't really have a child-parent structure in my html file.

      解决方案

      Try it this way:

      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>
      

      when applied to the following test input:

      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>
      

      the result will be:

      <?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>
      

      这篇关于使用 Xpath 选择每个节点和兄弟节点(直到该节点下一次出现)组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆