2011-03-16 53 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://hk.uwenku.com/question/p-rteaacsk-ub.html" target="_blank" title="XSL節點遍歷"> XSL節點遍歷 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-anhcivny-ge.html" target="_blank" title="如何遍歷XSL中的每個節點?"> 如何遍歷XSL中的每個節點? </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-cvtzbneh-dv.html" target="_blank" title="在多級樹形結構中循環遍歷父節點"> 在多級樹形結構中循環遍歷父節點 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-crwllago-bhg.html" target="_blank" title="XmlTextReader - 如何遍歷節點"> XmlTextReader - 如何遍歷節點 </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-nexsjlvd-bnh.html" target="_blank" title="XSL如何通過ID匹配值遞歸地遍歷節點"> XSL如何通過ID匹配值遞歸地遍歷節點 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-yoqssszb-mb.html" target="_blank" title="XML中的遍歷節點"> XML中的遍歷節點 </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-kimuhijc-nq.html" target="_blank" title="遍歷XSL"> 遍歷XSL </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-atxjhsmg-zb.html" target="_blank" title="Java:JGraphT:遍歷節點"> Java:JGraphT:遍歷節點 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-kgrztrnw-bnm.html" target="_blank" title="Networkx節點遍歷"> Networkx節點遍歷 </a> </li> <li> 10. <a href="http://hk.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://hk.uwenku.com/question/p-orrpyidn-yp.html" target="_blank" title="遍歷AST節點"> 遍歷AST節點 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-ewswytkm-uc.html" target="_blank" title="在xsl中循環遍歷嵌套節點"> 在xsl中循環遍歷嵌套節點 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-dfhxnrjh-eq.html" target="_blank" title="遍歷多級JTree的所有節點"> 遍歷多級JTree的所有節點 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-sxejgrqp-vk.html" target="_blank" title="如何遍歷XDocument的節點"> 如何遍歷XDocument的節點 </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-gaxdmqcn-xd.html" target="_blank" title="如何遍歷DevExpress.XtraTreeList.TreeList的節點"> 如何遍歷DevExpress.XtraTreeList.TreeList的節點 </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-mnlyaswt-em.html" target="_blank" title="如何遍歷樹的所有節點?"> 如何遍歷樹的所有節點? </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-qbjvsrjr-bhz.html" target="_blank" title="遍歷XML結構"> 遍歷XML結構 </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-zhkapcax-bbm.html" target="_blank" title="如何無損地遍歷數據結構 - 例如帶有節點的LinkedList?"> 如何無損地遍歷數據結構 - 例如帶有節點的LinkedList? </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-kjtorjrh-so.html" target="_blank" title="Python的結構遍歷多個項目"> Python的結構遍歷多個項目 </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-zpjznygc-mh.html" target="_blank" title="的XMLReader遍歷節點"> 的XMLReader遍歷節點 </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-tiiyhrnf-beg.html" target="_blank" title="如何在鏈表中創建多個節點,然後遍歷節點"> 如何在鏈表中創建多個節點,然後遍歷節點 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-azrnutet-wt.html" target="_blank" title="爲多級遍歷數據結構"> 爲多級遍歷數據結構 </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-fkzxtnqs-dn.html" target="_blank" title="如何遍歷這個JSON結構"> 如何遍歷這個JSON結構 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-epeuxlnu-ber.html" target="_blank" title="如何遍歷結構對象?"> 如何遍歷結構對象? </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-ogozcvxl-uo.html" target="_blank" title="如何遍歷這個數組結構"> 如何遍歷這個數組結構 </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-nqvohsjt-bhw.html" target="_blank" title="LibXML - 遍歷節點直到"> LibXML - 遍歷節點直到 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-vyubxcyb-mt.html" target="_blank" title="XSLT - 遍歷節點集"> XSLT - 遍歷節點集 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-nxdmbrob-uu.html" target="_blank" title="使用xpath遍歷節點"> 使用xpath遍歷節點 </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-hzxemxic-ee.html" target="_blank" title="Neo4j遍歷排除節點"> Neo4j遍歷排除節點 </a> </li> <li> 30. <a href="http://hk.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=1644592048261"></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"> <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://hk.uwenku.com/question/p-gtyakhcn-bkr.html" target="_blank" title="我如何從火力地堡/公司的FireStore通過屬性值排序文件"> 我如何從火力地堡/公司的FireStore通過屬性值排序文件 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-nonyplqr-bdz.html" target="_blank" title="Python - 從複雜對象的有序列表中刪除重複項?"> Python - 從複雜對象的有序列表中刪除重複項? </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-fwtmzlxr-bdt.html" target="_blank" title="python中的Xpath無法獲取數據"> python中的Xpath無法獲取數據 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-enzlmkpv-bep.html" target="_blank" title="檢查npm包是否通過終端安裝在package.json中"> 檢查npm包是否通過終端安裝在package.json中 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-hgkodxue-beg.html" target="_blank" title="更新現有資產 - Hyperledger作曲家"> 更新現有資產 - Hyperledger作曲家 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-aeekxncy-bgc.html" target="_blank" title="如何讓價值在2D或3D地圖在SCSS"> 如何讓價值在2D或3D地圖在SCSS </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-nhomiyry-bev.html" target="_blank" title="在JSP中的foreach"> 在JSP中的foreach </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-wbmoqksc-bgs.html" target="_blank" title="可以檢查一個集合或子集合是否存在?"> 可以檢查一個集合或子集合是否存在? </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-hzmoxnlc-bna.html" target="_blank" title="{reloadOnSearch:false}控制器在歷史記錄返回時不會重新加載"> {reloadOnSearch:false}控制器在歷史記錄返回時不會重新加載 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-ojrqfxds-bmt.html" target="_blank" title="使用SQL Server/C進行字母數字排序#"> 使用SQL Server/C進行字母數字排序# </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://hk.uwenku.com/question/p-rteaacsk-ub.html" target="_blank" title="XSL節點遍歷"> XSL節點遍歷 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-anhcivny-ge.html" target="_blank" title="如何遍歷XSL中的每個節點?"> 如何遍歷XSL中的每個節點? </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-cvtzbneh-dv.html" target="_blank" title="在多級樹形結構中循環遍歷父節點"> 在多級樹形結構中循環遍歷父節點 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-crwllago-bhg.html" target="_blank" title="XmlTextReader - 如何遍歷節點"> XmlTextReader - 如何遍歷節點 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.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://hk.uwenku.com/question/p-yoqssszb-mb.html" target="_blank" title="XML中的遍歷節點"> XML中的遍歷節點 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-kimuhijc-nq.html" target="_blank" title="遍歷XSL"> 遍歷XSL </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.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://hk.uwenku.com/question/p-kgrztrnw-bnm.html" target="_blank" title="Networkx節點遍歷"> Networkx節點遍歷 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.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://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.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>