XSLT 1.0 在 <xsl:for-each> 中使用 position()和 &lt;xsl:template&gt; [英] XSLT 1.0 Using position() in &lt;xsl:for-each&gt; and &lt;xsl:template&gt;

查看:39
本文介绍了XSLT 1.0 在 <xsl:for-each> 中使用 position()和 &lt;xsl:template&gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是, 的用法几乎达到了相同的目的,<xsl:for-each > 是一种匿名内联模板".

My understanding is that the usage of <xsl:template /> and <xsl:for-each> almost serve the same purpose and <xsl:for-each > is a sort of "anonymous inline template".

问题:但是,考虑到以下场景,我认为使用 更合适.请验证我的理解,或者有没有办法也可以通过 实现输出?

Question: However, considering the below scenario, i think using <xsl:for-each> is more appropriate. Please validate my understanding, or is there a way the output can be achieved through <xsl:template> as well?

输入 XML:

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book.child.1>
        <title>charithram</title>
        <author>sarika</author>
    </book.child.1>
    <book.child.2>
        <title>doublebell</title>
        <author>psudarsanan</author>
    </book.child.2>
</books>

预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<books>
   <book id="book1">
      <title>charithram</title>
      <author>sarika</author>
   </book>
   <book id="book2">
      <title>doublebell</title>
      <author>psudarsanan</author>
   </book>
</books>

XSLT1 [使用 <xsl:for-each >] - 这给出了预期的输出

XSLT1 [using <xsl:for-each >] - This gives the expected output

<?xml version="1.0" encoding="ISO-8859-1"?>
<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:template match="/">
        <newbooks>
            <xsl:for-each select="books/*">
            <newbook id="book{position()}"> 
                <title><xsl:value-of select="title" /></title>
                <author> <xsl:value-of select="author" /></author>
            </newbook>
          </xsl:for-each>
           </newbooks>
 </xsl:template>
</xsl:stylesheet>

XSLT2 [使用 <xsl:template >]

XSLT2 [using <xsl:template >]

<?xml version="1.0" encoding="ISO-8859-1"?>
<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:template match="/">
    <newbooks>
              <xsl:apply-templates/>    
     </newbooks>
 </xsl:template>
 <xsl:template match="books/*" >
    <newbook id="book{position()}"> 
        <title><xsl:value-of select="title" /></title>
        <author> <xsl:value-of select="author" /></author>
    </newbook>
 </xsl:template>
</xsl:stylesheet>

这并没有给出预期的输出,而是获得的输出是,

This does not give the expected output, Instead, the output obtained is,

<?xml version="1.0" encoding="UTF-8"?>
    <newbooks>
       <newbook id="book2">
          <title>charithram</title>
          <author>sarika</author>
       </newbook>
       <newbook id="book4">
          <title>doublebell</title>
          <author>psudarsanan</author>
       </newbook>
    </newbooks>

我能想到获得 2 和 4 的唯一原因可能是 id 节点在上下文中的位置.
position() 函数返回上下文节点在选定节点集中的位置".根据这个定义,它适用于 for-each 的情况,因为上下文是每个 元素.但是为什么它不适用于模板的情况?

The only reason i can think of getting 2 and 4, could be the position of id node inside the context.
"The position() function returns the position of the context node in a selected set of nodes". As per this definition, it works in the case of for-each as the context is each <book> element. But why it is not applicable in the case of template?

我也尝试过 <xsl:number> ,但无法使其按预期工作

I also tried with <xsl:number> , but cannot make it to work as expected

 <xsl:template match="/">
        <newbooks>
                  <xsl:apply-templates/>    
         </newbooks>
     </xsl:template>
     <xsl:template match="books/*" >
        <newbook >
            <xsl:attribute name="id">
              <xsl:text>book</xsl:text><xsl:number/>
            </xsl:attribute>
            <title><xsl:value-of select="title" /></title>
            <author> <xsl:value-of select="author" /></author>
        </newbook>
     </xsl:template>

我得到 book1, book1 的输出 [不递增]

I am getting the output of book1, book1 [not incrementing]

请帮忙.

注意:我使用 XSLT 转换为输出 XML,与输入 XML 相比,它具有完全不同的标签集,因此我不使用模板进行复制.

Note: I use XSLT to transform into output XML which has complete different set of tags compared to input XML, so I don't use a template to copy.

推荐答案

我认为您对差异的理解有点错误,但这不是这里的问题,因为您的 <xsl:apply-templates/> 本质上与 的作用相同.

I think your understanding of the differences is a bit faulty, but that is not the issue here, since your <xsl:apply-templates /> essentially does the same as an <xsl:for-each>.

但是:您在两个代码片段中使用的选择器存在重大差异.您的第一个示例使用 <xsl:for-each select="books/*">,它创建了一个基数为 2 的节点集,编号,毫不奇怪,1 和 2.您的第二个代码片段使用,它是 的简写,它创建基数为 5(!) 的节点集,节点 1、3 和 5 是文本节点(碰巧只包含空格),节点 2 和 4 是您实际想要定位的节点.

But: You have a major difference in the selectors you use in the two code snippets. Your first example uses <xsl:for-each select="books/*">, which creates a node set of cardinality two, numbered, unsurprisingly, 1 and 2. Your second code snippet uses <xsl:apply-templates />, which is a shorthand for <xsl:apply-templates select="node()">, and that creates a node set of cardinality five(!), with nodes 1, 3, and 5 being text nodes (that happen to only contain whitespace), and nodes 2 and 4 being the ones you actually want to target.

解决方案?将您的选择器添加到 apply-templates 元素,如 <xsl:apply-templates select="books/*"/>.

Solution? Add your selector to the apply-templates element, as in <xsl:apply-templates select="books/*" />.

EDIT 那么,为什么第二种情况下的节点集那么大?因为它选取了 books 的所有子节点,而且 XML 的工作方式不仅包括元素——它还包括文本节点(你确实想要你的 </code>> 和 <code><author></code> 有一个文本节点子节点)——<em>包括源自缩进的文本节点,</em> 只包含行尾和空格等内容和/或制表符,统称为空格.在这些输入变体上尝试原始的第二个代码:<em class="showen"></em></p> <p class="en"><strong>EDIT</strong> So, why is the node set in the second case that big? Because it picks up all child nodes of <code>books</code>, and the way XML works is that that includes not only elements – it also includes text nodes (you do want your <code><title></code> and <code><author></code> to have a text node child) – <em>including the text nodes that stem from your indenting,</em> which only contain things like end-of-line and spaces and/or tabs, collectively known as whitespace. Try the original second code on these input variations:</p> <pre><code><code><?xml version="1.0" encoding="UTF-8"?> <books> <book.child.1> <title>charithram</title> <author>sarika</author> </book.child.1> <book.child.2> <title>doublebell</title> <author>psudarsanan</author> </book.child.2> </books> </code></code></pre> <p class="cn">.</p> <pre><code><code><?xml version="1.0" encoding="UTF-8"?> <books><book.child.1> <title>charithram</title> <author>sarika</author> </book.child.1> <book.child.2> <title>doublebell</title> <author>psudarsanan</author> </book.child.2> </books> </code></code></pre> <p class="cn">.</p> <pre><code><code><?xml version="1.0" encoding="UTF-8"?> <books><book.child.1> <title>charithram</title> <author>sarika</author> </book.child.1><book.child.2> <title>doublebell</title> <author>psudarsanan</author> </book.child.2> </books> </code></code></pre> <p class="cn">另请注意,元素和文本并不是唯一的节点类型:<em class="showen"></em></p> <p class="en">Also note that elements and text are not the only types of nodes:</p> <pre><code><code><?xml version="1.0" encoding="UTF-8"?> <books> <!-- first book --> <book.child.1> <title>charithram</title> <author>sarika</author> </book.child.1> <!-- second book --> <book.child.2> <title>doublebell</title> <author>psudarsanan</author> </book.child.2> </books> </code></code></pre> <p class="cn">如果您有与这些匹配的模板(例如 <code><xsl:template match='text()'></code> 或更具体的东西),您会在那里找到相应的输出.XSLT 只是默认忽略应用模板中不匹配的节点.<em class="showen"></em></p> <p class="en">If you had templates matching these (such as an <code><xsl:template match='text()'></code> or maybe something more specific), you’d find the corresponding output there. XSLT just defaults to ignoring unmatched nodes in apply-templates.</p> <p>这篇关于XSLT 1.0 在 <xsl:for-each> 中使用 position()和 &lt;xsl:template&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!</p> </div> <div class="arc-body-main-more"> <span onclick="unlockarc('2505965');">查看全文</span> </div> </div> <div> </div> <div class="wwads-cn wwads-horizontal" data-id="166" style="max-width:100%;border: 4px solid #666;"></div> </div> </article> <div id="arc-ad-2" class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="widget bgwhite radius-1 mb-1 shadow widget-rel"> <h5>相关文章</h5> <ul> <li> <a target="_blank" title="XSL for-each" href="/1439701.html"> XSL for-each; </a> </li> <li> <a target="_blank" title="在xsl:for-each - newbie中使用document()" href="/1045222.html"> 在xsl:for-each - newbie中使用document(); </a> </li> <li> <a target="_blank" title="使用 xsl:variable 的 xsl:for-each 选择问题" href="/2503155.html"> 使用 xsl:variable 的 xsl:for-each 选择问题; </a> </li> <li> <a target="_blank" title="如何在 XSLT 中跳出 xsl:for-each 循环?" href="/2501122.html"> 如何在 XSLT 中跳出 xsl:for-each 循环?; </a> </li> <li> <a target="_blank" title="如何跳出 XSLT 中的 xsl:for-each 循环?" href="/2690703.html"> 如何跳出 XSLT 中的 xsl:for-each 循环?; </a> </li> <li> <a target="_blank" title="条件 &lt;xsl:output&gt;在 XSLT 1.0 中?" href="/2505586.html"> 条件 &lt;xsl:output&gt;在 XSLT 1.0 中?; </a> </li> <li> <a target="_blank" title="如何模拟xsl:for-each" href="/2881364.html"> 如何模拟xsl:for-each; </a> </li> <li> <a target="_blank" title="&lt;xsl:apply-template&gt; 之间的区别和 &lt;xsl:call-template&gt;?" href="/2501734.html"> &lt;xsl:apply-template&gt; 之间的区别和 &lt;xsl:call-template&gt;?; </a> </li> <li> <a target="_blank" title="在 xsl:for-each 中 xsl:value-of 的行为" href="/2505912.html"> 在 xsl:for-each 中 xsl:value-of 的行为; </a> </li> <li> <a target="_blank" title="xsl:for-each 有什么不好?" href="/2505554.html"> xsl:for-each 有什么不好?; </a> </li> <li> <a target="_blank" title="XSLT 参数的使用;&lt;xsl:param&gt;&amp;&lt;xsl:with-param&gt;" href="/2501169.html"> XSLT 参数的使用;&lt;xsl:param&gt;&amp;&lt;xsl:with-param&gt;; </a> </li> <li> <a target="_blank" title="如何在 xsl 中的另一个 for-each 中使用 for-each" href="/2502495.html"> 如何在 xsl 中的另一个 for-each 中使用 for-each; </a> </li> <li> <a target="_blank" title="我可以在xsl for-each中使用'and'运算符吗?" href="/2088707.html"> 我可以在xsl for-each中使用'and'运算符吗?; </a> </li> <li> <a target="_blank" title="替换 &lt;xsl:eval&gt;和 &lt;xsl:script&gt;在 XSL 中" href="/2505609.html"> 替换 &lt;xsl:eval&gt;和 &lt;xsl:script&gt;在 XSL 中; </a> </li> <li> <a target="_blank" title="xsl 中 for-each 和模板之间的区别?" href="/2667070.html"> xsl 中 for-each 和模板之间的区别?; </a> </li> <li> <a target="_blank" title="使用XSL中的"for-each"处理参数栈?" href="/1730332.html"> 使用XSL中的"for-each"处理参数栈?; </a> </li> <li> <a target="_blank" title="XSL xsl:template match="/";" href="/2500820.html"> XSL xsl:template match="/";; </a> </li> <li> <a target="_blank" title="for-each 中 if 语句的简单 xsl 问题" href="/2505618.html"> for-each 中 if 语句的简单 xsl 问题; </a> </li> <li> <a target="_blank" title="for-each和xsl中的模板之间的区别?" href="/769536.html"> for-each和xsl中的模板之间的区别?; </a> </li> <li> <a target="_blank" title="如何总结 XSL 中 for-each 循环的结果?" href="/2502071.html"> 如何总结 XSL 中 for-each 循环的结果?; </a> </li> <li> <a target="_blank" title="如何以相反的顺序执行 XSL:for-each" href="/2501370.html"> 如何以相反的顺序执行 XSL:for-each; </a> </li> <li> <a target="_blank" title="xsl:for-each循环内的计数器" href="/1580305.html"> xsl:for-each循环内的计数器; </a> </li> <li> <a target="_blank" title="xsl:for-each 循环中的计数器" href="/2649366.html"> xsl:for-each 循环中的计数器; </a> </li> <li> <a target="_blank" title="在 &lt;xsl:sort select=""/&gt; 中使用变量" href="/2501224.html"> 在 &lt;xsl:sort select=""/&gt; 中使用变量; </a> </li> <li> <a target="_blank" title="使用XPath选择正常序列xsl:for-each外的节点" href="/877209.html"> 使用XPath选择正常序列xsl:for-each外的节点; </a> </li> </ul> </div> <div class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="side"> <div class="widget widget-side bgwhite mb-1 shadow"> <h5>其他开发最新文章</h5> <ul> <li> <a target="_blank" title="拒绝显示一个框架,因为它将'X-Frame-Options'设置为'sameorigin'" href="/893060.html"> 拒绝显示一个框架,因为它将'X-Frame-Options'设置为'sameorigin'; </a> </li> <li> <a target="_blank" title="什么是&QUOT; AW&QUOT;在部分标志属性是什么意思?" href="/303988.html"> 什么是&QUOT; AW&QUOT;在部分标志属性是什么意思?; </a> </li> <li> <a target="_blank" title="在运行npm install命令时获取'npm WARN弃用'警告" href="/840917.html"> 在运行npm install命令时获取'npm WARN弃用'警告; </a> </li> <li> <a target="_blank" title="cmake无法找到openssl" href="/516280.html"> cmake无法找到openssl; </a> </li> <li> <a target="_blank" title="从Spark的scala中的* .tar.gz压缩文件中读取HDF5文件" href="/850628.html"> 从Spark的scala中的* .tar.gz压缩文件中读取HDF5文件; </a> </li> <li> <a target="_blank" title="Twitter :: Error :: Forbidden - 无法验证您的凭据" href="/630061.html"> Twitter :: Error :: Forbidden - 无法验证您的凭据; </a> </li> <li> <a target="_blank" title="我什么时候需要一个fb:app_id或者fb:admins?" href="/747981.html"> 我什么时候需要一个fb:app_id或者fb:admins?; </a> </li> <li> <a target="_blank" title="将.db文件导入R" href="/902960.html"> 将.db文件导入R; </a> </li> <li> <a target="_blank" title="npm通知创建一个lockfile作为package-lock.json。你应该提交这个文件" href="/744854.html"> npm通知创建一个lockfile作为package-lock.json。你应该提交这个文件; </a> </li> <li> <a target="_blank" title="拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src'self'”" href="/819167.html"> 拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src'self'”; </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门教程 </h5> <ul> <li> <a target="_blank" title="Java教程" href="/OnLineTutorial/java/index.html"> Java教程 </a> </li> <li> <a target="_blank" title="Apache ANT 教程" href="/OnLineTutorial/ant/index.html"> Apache ANT 教程 </a> </li> <li> <a target="_blank" title="Kali Linux教程" href="/OnLineTutorial/kali_linux/index.html"> Kali Linux教程 </a> </li> <li> <a target="_blank" title="JavaScript教程" href="/OnLineTutorial/javascript/index.html"> JavaScript教程 </a> </li> <li> <a target="_blank" title="JavaFx教程" href="/OnLineTutorial/javafx/index.html"> JavaFx教程 </a> </li> <li> <a target="_blank" title="MFC 教程" href="/OnLineTutorial/mfc/index.html"> MFC 教程 </a> </li> <li> <a target="_blank" title="Apache HTTP客户端教程" href="/OnLineTutorial/apache_httpclient/index.html"> Apache HTTP客户端教程 </a> </li> <li> <a target="_blank" title="Microsoft Visio 教程" href="/OnLineTutorial/microsoft_visio/index.html"> Microsoft Visio 教程 </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门工具 </h5> <ul> <li> <a target="_blank" title="Java 在线工具" href="/Onlinetools/details/4"> Java 在线工具 </a> </li> <li> <a target="_blank" title="C(GCC) 在线工具" href="/Onlinetools/details/6"> C(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="PHP 在线工具" href="/Onlinetools/details/8"> PHP 在线工具 </a> </li> <li> <a target="_blank" title="C# 在线工具" href="/Onlinetools/details/1"> C# 在线工具 </a> </li> <li> <a target="_blank" title="Python 在线工具" href="/Onlinetools/details/5"> Python 在线工具 </a> </li> <li> <a target="_blank" title="MySQL 在线工具" href="/Onlinetools/Dbdetails/33"> MySQL 在线工具 </a> </li> <li> <a target="_blank" title="VB.NET 在线工具" href="/Onlinetools/details/2"> VB.NET 在线工具 </a> </li> <li> <a target="_blank" title="Lua 在线工具" href="/Onlinetools/details/14"> Lua 在线工具 </a> </li> <li> <a target="_blank" title="Oracle 在线工具" href="/Onlinetools/Dbdetails/35"> Oracle 在线工具 </a> </li> <li> <a target="_blank" title="C++(GCC) 在线工具" href="/Onlinetools/details/7"> C++(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="Go 在线工具" href="/Onlinetools/details/20"> Go 在线工具 </a> </li> <li> <a target="_blank" title="Fortran 在线工具" href="/Onlinetools/details/45"> Fortran 在线工具 </a> </li> </ul> </div> </div> </div> <script type="text/javascript">var eskeys = 'xslt,1.0,在,xsl,for-each,中,使用,position,和,lt,xsl,template&gt'; var cat = 'cc';';//other-dev</script> </div> <div id="pop" onclick="pophide();"> <div id="pop_body" onclick="event.stopPropagation();"> <h6 class="flex flex101"> 登录 <span onclick="pophide();">关闭</span> </h6> <div class="pd-1"> <div class="wxtip center"> <span>扫码关注<em>1秒</em>登录</span> </div> <div class="center"> <img id="qr" src="https://huajiakeji.com/Content/Images/qrydx.jpg" alt="" style="width:150px;height:150px;" /> </div> <div style="margin-top:10px;display:flex;justify-content: center;"> <input type="text" placeholder="输入验证码" id="txtcode" autocomplete="off" /> <input id="btngo" type="button" onclick="chk()" value="GO" /> </div> <div class="center" style="margin: 4px; font-size: .8rem; color: #f60;"> 发送“验证码”获取 <em style="padding: 0 .5rem;">|</em> <span style="color: #01a05c;">15天全站免登陆</span> </div> <div id="chkinfo" class="tip"></div> </div> </div> </div> <script type="text/javascript" src="https://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/highlight.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/base.js?v=0.22"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/tui.js?v=0.11"></script> <footer class="footer"> <div class="container"> <div class="flink mb-1"> 友情链接: <a href="https://www.it1352.com/" target="_blank">IT屋</a> <a href="https://huajiakeji.com/" target="_blank">Chrome插件</a> <a href="https://www.cnplugins.com/" target="_blank">谷歌浏览器插件</a> </div> <section class="copyright-section"> <a href="https://www.it1352.com" title="IT屋-程序员软件开发技术分享社区">IT屋</a> ©2016-2022 <a href="http://www.beian.miit.gov.cn/" target="_blank">琼ICP备2021000895号-1</a> <a href="/sitemap.html" target="_blank" title="站点地图">站点地图</a> <a href="/Home/Tags" target="_blank" title="站点标签">站点标签</a> <a target="_blank" alt="sitemap" href="/sitemap.xml">SiteMap</a> <a href="/1155981.html" title="IT屋-免责申明"><免责申明></a> 本站内容来源互联网,如果侵犯您的权益请联系我们删除. </section> <!--统计代码--> <script type="text/javascript"> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?0c3a090f7b3c4ad458ac1296cb5cc779"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script type="text/javascript"> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </div> </footer> </body> </html>