为什么模板模式中不允许使用后代或自我::? [英] Why descendant-or-self:: is not allowed in template pattern?

查看:21
本文介绍了为什么模板模式中不允许使用后代或自我::?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下 XML(它是 TEI 注释方案转换为 HTML):

<p>(例如,参见 <bibl type="journal" xmlns="http://www.tei-c.org/ns/1.0"><author>GregerIH 等人</author><date>2007</date>,<title>Trends Neurosci.</title><biblScope type="vol">30</biblScope> (</biblScope>biblScope type="issue">8</biblScope>):<biblScope type="pp">407-16</biblScope></bibl>).</p>

现在我想将所有注释节点按原样复制到生成的 XHTML 中,但只将 </code> 重命名为 <code><bibTitle></code> (因为 <code><title></code> 只允许在 <code><head></code> 中),所以我使用了以下转换:</p><pre><code><xsl:template match="tei:bibl/descendant-or-self::*"><xsl:variable name="nodeName"><xsl:选择><xsl:when test="name() = 'title'">bibTitle</xsl:when><xsl:otherwise><xsl:value-of select="name()"/></xsl:otherwise></xsl:选择></xsl:变量><!-- 命名空间的改变发生在这里,但我们不在乎--><xsl:element name="{$nodeName}"><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:element></xsl:模板><xsl:template match="p/text()|tei:bibl//text()"><xsl:copy-of select="."/></xsl:模板></code></pre><p>但是它无法编译并因以下错误而中断:</p><p><code>在匹配模式中只允许 child:: 和 attribute:: 轴!违规轴 = 后代或自我</code></p><p>当我将匹配规则更改为 <code><xsl:template match="tei:bibl|tei:bibl//*"></code> 时,它开始按预期工作.但这应该与 <code>descendant-or-self::*</code> 相同,对吗?我在这里遇到了转换器实现限制吗?</p><p>首先,我使用 Mozilla 3.5 内部转换器进行了测试,然后使用 Xalan 2.7.1 进行了测试 - 结果相同.</p><div class="h2_lin"> 解决方案 </div><p><strong>此限制<em>仅</em>对模板匹配模式中的任何<em>位置步骤</em>有效</em>强>.这是设计使然(由 W3C <strong><a href="http://www.w3.org/TR/xslt#patterns" rel="nofollow">XSLT 1.0</a></strong> 和<strong><a href="http://www.w3.org/TR/2007/REC-xslt20-20070123/#pattern-syntax" rel="nofollow">XSLT 2.0</a></strong> 规范) -- 确保高效的 XSLT 处理.</p><p><strong>注意</strong>:可以在任何位置步骤之后的谓词中自由使用任何轴(包括<code>descending-or-self::</code>).</p><p><strong>更新</strong>:</p><p>以下是在 <code>xsl:template</code> 的 <code>match</code> 属性中使用 <code>descendant-or-self::</code> 轴的简短完整示例:</p><pre><code><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><xsl:template match="num[descendant-or-self::num > 5]"/></xsl:stylesheet></code></pre><p><strong>当此转换应用于以下 XML 文档时</strong>:</p><pre><code><nums><num>1</num><num>2</num><num>3</num><num>4</num><num>5</num><num>6</num><num>7</num><num>8</num><num>9</num><num>10</num></nums></code></pre><p><strong>想要的结果:删除值 >= 5 的任何 <code>num</code> 个元素:</strong></p><pre><code><nums><num>1</num><num>2</num><num>3</num><num>4</num><num>5</num></nums></code></pre><p><p>Suppose I have the following XML (which is the embedding of <a href="http://www.tei-c.org/release/doc/tei-p5-doc/en/html/ref-bibl.html" rel="nofollow">TEI</a> annotation scheme into HTML):</p> <p><code><p>(See, for example, <bibl type="journal" xmlns="http://www.tei-c.org/ns/1.0"><author>Greger IH, et al.</author> <date>2007</date>, <title>Trends Neurosci.</title> <biblScope type="vol">30</biblScope> (<biblScope type="issue">8</biblScope>): <biblScope type="pp">407-16</biblScope></bibl>).</p></code></p> <p>Now I want to copy all annotation nodes <em>as is</em> into resulting XHTML but only rename <code><title></code> to <code><bibTitle></code> (as <code><title></code> is only allowed in <code><head></code>), so I used the following transformation:</p><pre><code><xsl:template match="tei:bibl/descendant-or-self::*"> <xsl:variable name="nodeName"> <xsl:choose> <xsl:when test="name() = 'title'">bibTitle</xsl:when> <xsl:otherwise><xsl:value-of select="name()" /></xsl:otherwise> </xsl:choose> </xsl:variable> <!-- Changing of the namespace occurs here, but we don't care --> <xsl:element name="{$nodeName}"> <xsl:copy-of select="@*" /> <xsl:apply-templates /> </xsl:element> </xsl:template> <xsl:template match="p/text()|tei:bibl//text()"> <xsl:copy-of select="." /> </xsl:template> </code></pre><p>However it does not compile and breaks with following error:</p> <p><code>Only child:: and attribute:: axes are allowed in match patterns! Offending axes = descendant-or-self</code></p> <p>When I change the match rule to <code><xsl:template match="tei:bibl|tei:bibl//*"></code> it starts working as intended. But that should be identical to <code>descendant-or-self::*</code>, right? Have I hit the transformer implementation limitation here?</p> <p>First I've tested with Mozilla 3.5 internal transformer, then with Xalan 2.7.1 – same negative result.</p><div class="h2_lin"> 解决方案 </div><p><strong>This limitation is valid <em>only</em> for any <em>location step</em> within the template's match pattern</strong>. It is by design (mandated by the W3C <strong><a href="http://www.w3.org/TR/xslt#patterns" rel="nofollow">XSLT 1.0</a></strong> and <strong><a href="http://www.w3.org/TR/2007/REC-xslt20-20070123/#pattern-syntax" rel="nofollow">XSLT 2.0</a></strong> specifications) -- to ensure efficient XSLT processing.</p> <p><strong>Do note</strong>: One can freely use any axis (including <code>descending-or-self::</code>) withinin the predicates that follow any location step.</p> <p><strong>Update</strong>:</p> <p>Here is a short, complete example of using the <code>descendant-or-self::</code> axis in the <code>match</code> attribute of <code>xsl:template</code>:</p><pre><code><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="num[descendant-or-self::num > 5]"/> </xsl:stylesheet> </code></pre><p><strong>when this transformation is applied on the following XML document</strong>:</p><pre><code><nums> <num>1</num> <num>2</num> <num>3</num> <num>4</num> <num>5</num> <num>6</num> <num>7</num> <num>8</num> <num>9</num> <num>10</num> </nums> </code></pre><p><strong>the wanted result: any <code>num</code> elements with value >= 5 are deleted:</strong></p><pre><code><nums> <num>1</num> <num>2</num> <num>3</num> <num>4</num> <num>5</num> </nums> </code></pre><p> <p>这篇关于为什么模板模式中不允许使用后代或自我::?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!</p> </div> <div class="arc-body-main-more"> <span onclick="unlockarc('2505714');">查看全文</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="为什么在替代模式中不允许使用变量?" href="/2331285.html"> 为什么在替代模式中不允许使用变量?; </a> </li> <li> <a target="_blank" title="为什么不允许在“使用"模式中使用模式?绑定?" href="/2074868.html"> 为什么不允许在“使用"模式中使用模式?绑定?; </a> </li> <li> <a target="_blank" title="为什么不允许此默认模板参数?" href="/479644.html"> 为什么不允许此默认模板参数?; </a> </li> <li> <a target="_blank" title="为什么在模板定义中不允许使用结构体?" href="/489471.html"> 为什么在模板定义中不允许使用结构体?; </a> </li> <li> <a target="_blank" title="模板文本中不允许#{...}" href="/1501588.html"> 模板文本中不允许#{...}; </a> </li> <li> <a target="_blank" title="为什么不允许乘法?" href="/1026533.html"> 为什么不允许乘法?; </a> </li> <li> <a target="_blank" title="为什么不允许使用"constexpr"参数?" href="/2210799.html"> 为什么不允许使用"constexpr"参数?; </a> </li> <li> <a target="_blank" title="为什么Hashtable不允许空键或值?" href="/846400.html"> 为什么Hashtable不允许空键或值?; </a> </li> <li> <a target="_blank" title="为什么不允许nameof(object)?" href="/1870958.html"> 为什么不允许nameof(object)?; </a> </li> <li> <a target="_blank" title="为什么不允许复制stringstream?" href="/453360.html"> 为什么不允许复制stringstream?; </a> </li> <li> <a target="_blank" title="HTMLBars模板中不允许使用“SCRIPT”标签" href="/678034.html"> HTMLBars模板中不允许使用“SCRIPT”标签; </a> </li> <li> <a target="_blank" title="为什么tslint:recommended不允许使用模块?" href="/1632175.html"> 为什么tslint:recommended不允许使用模块?; </a> </li> <li> <a target="_blank" title="为什么PHP属性不允许使用函数?" href="/1538101.html"> 为什么PHP属性不允许使用函数?; </a> </li> <li> <a target="_blank" title="为什么不允许double作为非类型模板参数?" href="/1986200.html"> 为什么不允许double作为非类型模板参数?; </a> </li> <li> <a target="_blank" title="为什么模板模板参数不允许'typename'在参数列表后面" href="/457487.html"> 为什么模板模板参数不允许'typename'在参数列表后面; </a> </li> <li> <a target="_blank" title="为什么java中不允许赋值给'this'?" href="/961345.html"> 为什么java中不允许赋值给'this'?; </a> </li> <li> <a target="_blank" title="为什么 WCF 中不允许方法重载?" href="/2667164.html"> 为什么 WCF 中不允许方法重载?; </a> </li> <li> <a target="_blank" title="为什么抽象允许修改?为什么界面不允许修改" href="/1097035.html"> 为什么抽象允许修改?为什么界面不允许修改; </a> </li> <li> <a target="_blank" title="为什么 Hashtable 不允许空键或空值?" href="/2785029.html"> 为什么 Hashtable 不允许空键或空值?; </a> </li> <li> <a target="_blank" title="xpath 使用//和后代或自我和自我" href="/2501929.html"> xpath 使用//和后代或自我和自我; </a> </li> <li> <a target="_blank" title="为什么 PL/SQL 中不允许使用静态 ddl?" href="/2336526.html"> 为什么 PL/SQL 中不允许使用静态 ddl?; </a> </li> <li> <a target="_blank" title="为什么HTML中不允许使用重复的ID" href="/1025257.html"> 为什么HTML中不允许使用重复的ID; </a> </li> <li> <a target="_blank" title="为什么方法中不允许使用静态本地类?" href="/985152.html"> 为什么方法中不允许使用静态本地类?; </a> </li> <li> <a target="_blank" title="为什么在C ++中不允许使用引用数组" href="/1190172.html"> 为什么在C ++中不允许使用引用数组; </a> </li> <li> <a target="_blank" title="为什么在类中不允许函数模板特殊化?" href="/479632.html"> 为什么在类中不允许函数模板特殊化?; </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 = '为什么,模板,模式,中,不允许,使用,后代,或,自我'; 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>