2011-03-16 27 views
0

我需要遍历多节点结构,但该结构只能由前导节点标识,并且该前导节点之后的节点可能会有所不同。如何遍历XSL中的多节点结构

在这个例子中,我需要通过每个<title>-some_nodes<title>-some_other_nodes块迭代

XML文件:

<books> 
    <title>book_one</title> 
    <price>price_for_book_one</price> 
    <notes>notes_for_book_one</notes> 

    <title>book_two</title> 
    <price>price_for_book_two</price> 
</books> 

所需的输出:

<div class="book"> 
    <h1 id="title">book_one</h1> 
    <h2 id="values"> 
     <p>price_for_book_one</p> 
     <p>notes_for_book_one</p> 
    </h2> 
</div> 

<div class="book"> 
    <h1 id="title">book_two</h1> 
    <h2 id="values"> 
     <p>price_for_book_two</p> 
    </h2> 
</div> 

我尝试:

<xsl:template match="/"> 
    <xsl:for-each select="books/title"> 
     <h1 id="title"><xsl:text>Title:</xsl:text> 
      <xsl:value-of select="." /> 
     </h1> 

     <h2 id="values">  
     <!-- have other templates for matching all possible following-sibling nodes --> 
      <xsl:apply-templates select="following-sibling::*> 
     </h2> 
    </xsl:for-each> 
</xsl:template> 

但是这只会选择标题节点而不解析任何其他节点,因为“books/title”只选择标题节点。

注:<price><notes>是两个示例节点,有可能是在<title>节点之间的任何东西,我有处理它们的其他模板,但如何选择它们是我的问题。

任何想法将不胜感激。

+0

你忘了解释什么“不真的窝窝rk“...请编辑您的问题,并提供您需要的确切正确结果以及您实际得到的结果。另外,请描述实际结果究竟出了什么问题。 – 2011-03-16 02:15:19

+0

感谢您的反馈,编辑过的问题。 – highlightall 2011-03-17 20:23:25

回答

1

在你的循环中,上下文节点是title,所以用.来选择它。

您可能只想要标题后面的第一个价格和备注元素。我还使用了xsl:text来减少每个段落中的空白。

<xsl:template match="/"> 
    <xsl:for-each select="books/title"> 
     <p> 
      <xsl:text>Title: </xsl:text> 
      <xsl:value-of select="." /> 
     </p> 
     <p> 
      <xsl:text>Price: </xsl:text> 
      <xsl:value-of select="following-sibling::price[1]"/> 
     </p> 
     <p> 
      <xsl:text>Notes: </xsl:text> 
      <xsl:value-of select="following-sibling::notes[1]"/> 
     </p> 
    </xsl:for-each> 
</xsl:template> 

如果每个title之间的额外的节点是不固定的,你可以使用模板处理它们。这里最重要的是XPath表达式来选择属于当前标题的非标题元素。

<xsl:template match="/"> 
    <xsl:for-each select="books/title"> 
     <p> 
      <xsl:text>Title:</xsl:text> 
      <xsl:value-of select="." /> 
     </p> 
     <xsl:apply-templates select="following-sibling::*[ 
             local-name() != 'title' and 
             preceding-sibling::title[1] = current()]"/> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="price"> 
    <p> 
     <xsl:text>Price: </xsl:text> 
     <xsl:value-of select="."/> 
    </p> 
</xsl:template> 
+0

感谢您的回答!我将编辑选择标题节点的模板。 但是,之后的节点未确定,它可能是其他节点,而不是<price>和<notes>。 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/286549/">highlightall</a></span> <span>2011-03-16 01:28:46</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">这第二个解决方案正是我一直在寻找的!非常感谢! – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/286549/">highlightall</a></span> <span>2011-03-17 20:08:12</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">1<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>首先,您似乎不需要分组。这个样式表:</p> <pre><code class="prettyprint-override"><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="books/*"> <p> <xsl:value-of select="concat(translate( substring(local-name(),1,1), 'qwertyuiopasdfghjklzxcvbnm', 'QWERTYUIOPASDFGHJKLZXCVBNM' ), substring(local-name(),2), ': ', . )" /> </p> </xsl:template> </xsl:stylesheet> </code></pre> <p>输出:</p> <pre><code class="prettyprint-override"><p>Title: book_one</p> <p>Price: price_for_book_one</p> <p>Notes: notes_for_book_one</p> <p>Title: book_two</p> <p>Price: price_for_book_two</p> </code></pre> <p>分组将只需要如果你打算做一些跟团,像包木窗:</p> <pre><code class="prettyprint-override"><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:key name="kElementByPrecedingTitle" match="books/*" use="generate-id((.|preceding-sibling::*)[self::title][last()])"/> <xsl:template match="books/*"> <p> <xsl:value-of select="concat(translate( substring(local-name(),1,1), 'qwertyuiopasdfghjklzxcvbnm', 'QWERTYUIOPASDFGHJKLZXCVBNM' ), substring(local-name(),2), ': ', . )" /> </p> </xsl:template> <xsl:template match="books"> <xsl:for-each select="title"> <div class="book"> <xsl:apply-templates select="key('kElementByPrecedingTitle',generate-id())"/> </div> </xsl:for-each> </xsl:template> </xsl:stylesheet> </code></pre> <p>输出:</p> <pre><code class="prettyprint-override"><div class="book"> <p>Title: book_one</p> <p>Price: price_for_book_one</p> <p>Notes: notes_for_book_one</p> </div> <div class="book"> <p>Title: book_two</p> <p>Price: price_for_book_two</p> </div> </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/5330792">来源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2011-03-16 19:36:42</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">对不起,我不是很清楚我正在寻找的输出。我确实需要标记每个组,并且还需要单独标记<title>节点,然后将该组的其余部分标记到另一个标记中。 使用密钥的好例子,绝对值得了解。谢谢! – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/286549/">highlightall</a></span> <span>2011-03-17 20:10:05</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相关问题</div> <ul class="relative_list"> <li> 1. <a href="http://www.uwenku.com/question/p-rteaacsk-ub.html" target="_blank" title="XSL节点遍历"> XSL节点遍历 </a> </li> <li> 2. <a href="http://www.uwenku.com/question/p-anhcivny-ge.html" target="_blank" title="如何遍历XSL中的每个节点?"> 如何遍历XSL中的每个节点? </a> </li> <li> 3. <a href="http://www.uwenku.com/question/p-cvtzbneh-dv.html" target="_blank" title="在多级树形结构中循环遍历父节点"> 在多级树形结构中循环遍历父节点 </a> </li> <li> 4. <a href="http://www.uwenku.com/question/p-crwllago-bhg.html" target="_blank" title="XmlTextReader - 如何遍历节点"> XmlTextReader - 如何遍历节点 </a> </li> <li> 5. <a href="http://www.uwenku.com/question/p-nexsjlvd-bnh.html" target="_blank" title="XSL如何通过ID匹配值递归地遍历节点"> XSL如何通过ID匹配值递归地遍历节点 </a> </li> <li> 6. <a href="http://www.uwenku.com/question/p-yoqssszb-mb.html" target="_blank" title="XML中的遍历节点"> XML中的遍历节点 </a> </li> <li> 7. <a href="http://www.uwenku.com/question/p-kimuhijc-nq.html" target="_blank" title="遍历XSL"> 遍历XSL </a> </li> <li> 8. <a href="http://www.uwenku.com/question/p-atxjhsmg-zb.html" target="_blank" title="Java:JGraphT:遍历节点"> Java:JGraphT:遍历节点 </a> </li> <li> 9. <a href="http://www.uwenku.com/question/p-kgrztrnw-bnm.html" target="_blank" title="Networkx节点遍历"> Networkx节点遍历 </a> </li> <li> 10. <a href="http://www.uwenku.com/question/p-ftlbxuqj-os.html" target="_blank" title="ANTLR - 遍历节点"> ANTLR - 遍历节点 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://www.uwenku.com/question/p-orrpyidn-yp.html" target="_blank" title="遍历AST节点"> 遍历AST节点 </a> </li> <li> 12. <a href="http://www.uwenku.com/question/p-ewswytkm-uc.html" target="_blank" title="在xsl中循环遍历嵌套节点"> 在xsl中循环遍历嵌套节点 </a> </li> <li> 13. <a href="http://www.uwenku.com/question/p-dfhxnrjh-eq.html" target="_blank" title="遍历多级JTree的所有节点"> 遍历多级JTree的所有节点 </a> </li> <li> 14. <a href="http://www.uwenku.com/question/p-sxejgrqp-vk.html" target="_blank" title="如何遍历XDocument的节点"> 如何遍历XDocument的节点 </a> </li> <li> 15. <a href="http://www.uwenku.com/question/p-gaxdmqcn-xd.html" target="_blank" title="如何遍历DevExpress.XtraTreeList.TreeList的节点"> 如何遍历DevExpress.XtraTreeList.TreeList的节点 </a> </li> <li> 16. <a href="http://www.uwenku.com/question/p-mnlyaswt-em.html" target="_blank" title="如何遍历树的所有节点?"> 如何遍历树的所有节点? </a> </li> <li> 17. <a href="http://www.uwenku.com/question/p-qbjvsrjr-bhz.html" target="_blank" title="遍历XML结构"> 遍历XML结构 </a> </li> <li> 18. <a href="http://www.uwenku.com/question/p-zhkapcax-bbm.html" target="_blank" title="如何无损地遍历数据结构 - 例如带有节点的LinkedList?"> 如何无损地遍历数据结构 - 例如带有节点的LinkedList? </a> </li> <li> 19. <a href="http://www.uwenku.com/question/p-kjtorjrh-so.html" target="_blank" title="Python的结构遍历多个项目"> Python的结构遍历多个项目 </a> </li> <li> 20. <a href="http://www.uwenku.com/question/p-zpjznygc-mh.html" target="_blank" title="的XMLReader遍历节点"> 的XMLReader遍历节点 </a> </li> <li> 21. <a href="http://www.uwenku.com/question/p-tiiyhrnf-beg.html" target="_blank" title="如何在链表中创建多个节点,然后遍历节点"> 如何在链表中创建多个节点,然后遍历节点 </a> </li> <li> 22. <a href="http://www.uwenku.com/question/p-azrnutet-wt.html" target="_blank" title="为多级遍历数据结构"> 为多级遍历数据结构 </a> </li> <li> 23. <a href="http://www.uwenku.com/question/p-fkzxtnqs-dn.html" target="_blank" title="如何遍历这个JSON结构"> 如何遍历这个JSON结构 </a> </li> <li> 24. <a href="http://www.uwenku.com/question/p-epeuxlnu-ber.html" target="_blank" title="如何遍历结构对象?"> 如何遍历结构对象? </a> </li> <li> 25. <a href="http://www.uwenku.com/question/p-ogozcvxl-uo.html" target="_blank" title="如何遍历这个数组结构"> 如何遍历这个数组结构 </a> </li> <li> 26. <a href="http://www.uwenku.com/question/p-nqvohsjt-bhw.html" target="_blank" title="LibXML - 遍历节点直到"> LibXML - 遍历节点直到 </a> </li> <li> 27. <a href="http://www.uwenku.com/question/p-vyubxcyb-mt.html" target="_blank" title="XSLT - 遍历节点集"> XSLT - 遍历节点集 </a> </li> <li> 28. <a href="http://www.uwenku.com/question/p-nxdmbrob-uu.html" target="_blank" title="使用xpath遍历节点"> 使用xpath遍历节点 </a> </li> <li> 29. <a href="http://www.uwenku.com/question/p-hzxemxic-ee.html" target="_blank" title="Neo4j遍历排除节点"> Neo4j遍历排除节点 </a> </li> <li> 30. <a href="http://www.uwenku.com/question/p-uivrycuj-ht.html" target="_blank" title="AS3 XML节点遍历"> AS3 XML节点遍历 </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048176"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 每日一句 </div> <div class="panel-body m-b-sm m-t-sm clearfix"> 每一个你不满意的现在,都有一个你没有努力的曾经。 </div> </div> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新问题 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://www.uwenku.com/question/p-cywfgejx-ue.html" target="_blank" title="总结各行的第一n个元素中的2D阵列方案"> 总结各行的第一n个元素中的2D阵列方案 </a> </li> <li class="side_article_list_item"> 2. <a href="http://www.uwenku.com/question/p-vlhhxkja-vq.html" target="_blank" title="stm32f411 HAL ssd1289"> stm32f411 HAL ssd1289 </a> </li> <li class="side_article_list_item"> 3. <a href="http://www.uwenku.com/question/p-opzqpfeb-sg.html" target="_blank" title="如何使用JpaRepository和嵌套的对象列表进行搜索?"> 如何使用JpaRepository和嵌套的对象列表进行搜索? </a> </li> <li class="side_article_list_item"> 4. <a href="http://www.uwenku.com/question/p-eroolcyc-ss.html" target="_blank" title="加载视图从XIB和获取零的MacOS应用程序"> 加载视图从XIB和获取零的MacOS应用程序 </a> </li> <li class="side_article_list_item"> 5. <a href="http://www.uwenku.com/question/p-nusirtqs-tb.html" target="_blank" title="简单HyperHtmlElement例子说明不了什么"> 简单HyperHtmlElement例子说明不了什么 </a> </li> <li class="side_article_list_item"> 6. <a href="http://www.uwenku.com/question/p-shtcsbft-tn.html" target="_blank" title="如何从用户输入来搜索django/python中的内容?"> 如何从用户输入来搜索django/python中的内容? </a> </li> <li class="side_article_list_item"> 7. <a href="http://www.uwenku.com/question/p-ycsjsmsj-qh.html" target="_blank" title="如何与共享相同字段的实体建立数据库关系"> 如何与共享相同字段的实体建立数据库关系 </a> </li> <li class="side_article_list_item"> 8. <a href="http://www.uwenku.com/question/p-tflwwwvl-qt.html" target="_blank" title="将函数赋值给使用“this”引用自身的数组对象"> 将函数赋值给使用“this”引用自身的数组对象 </a> </li> <li class="side_article_list_item"> 9. <a href="http://www.uwenku.com/question/p-hbezvefi-rc.html" target="_blank" title="文本到莫尔斯电码在C"> 文本到莫尔斯电码在C </a> </li> <li class="side_article_list_item"> 10. <a href="http://www.uwenku.com/question/p-wuodurlo-ro.html" target="_blank" title="Vaadin上传功能"> Vaadin上传功能 </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相关问题</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://www.uwenku.com/question/p-rteaacsk-ub.html" target="_blank" title="XSL节点遍历"> XSL节点遍历 </a> </li> <li class="side_article_list_item"> 2. <a href="http://www.uwenku.com/question/p-anhcivny-ge.html" target="_blank" title="如何遍历XSL中的每个节点?"> 如何遍历XSL中的每个节点? </a> </li> <li class="side_article_list_item"> 3. <a href="http://www.uwenku.com/question/p-cvtzbneh-dv.html" target="_blank" title="在多级树形结构中循环遍历父节点"> 在多级树形结构中循环遍历父节点 </a> </li> <li class="side_article_list_item"> 4. <a href="http://www.uwenku.com/question/p-crwllago-bhg.html" target="_blank" title="XmlTextReader - 如何遍历节点"> XmlTextReader - 如何遍历节点 </a> </li> <li class="side_article_list_item"> 5. <a href="http://www.uwenku.com/question/p-nexsjlvd-bnh.html" target="_blank" title="XSL如何通过ID匹配值递归地遍历节点"> XSL如何通过ID匹配值递归地遍历节点 </a> </li> <li class="side_article_list_item"> 6. <a href="http://www.uwenku.com/question/p-yoqssszb-mb.html" target="_blank" title="XML中的遍历节点"> XML中的遍历节点 </a> </li> <li class="side_article_list_item"> 7. <a href="http://www.uwenku.com/question/p-kimuhijc-nq.html" target="_blank" title="遍历XSL"> 遍历XSL </a> </li> <li class="side_article_list_item"> 8. <a href="http://www.uwenku.com/question/p-atxjhsmg-zb.html" target="_blank" title="Java:JGraphT:遍历节点"> Java:JGraphT:遍历节点 </a> </li> <li class="side_article_list_item"> 9. <a href="http://www.uwenku.com/question/p-kgrztrnw-bnm.html" target="_blank" title="Networkx节点遍历"> Networkx节点遍历 </a> </li> <li class="side_article_list_item"> 10. <a href="http://www.uwenku.com/question/p-ftlbxuqj-os.html" target="_blank" title="ANTLR - 遍历节点"> ANTLR - 遍历节点 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://www.uwenku.com/contact">联系我们</a></li> <li>© 2020 UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>