首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在键之间共享匹配模式?

如何在键之间共享匹配模式?
EN

Stack Overflow用户
提问于 2013-06-22 05:16:58
回答 2查看 60关注 0票数 4

我有两个具有相同匹配模式的密钥。这个模式很长。模式本身并不重要;问题在于长时间的重复:

代码语言:javascript
复制
  <xsl:key name="narrow-things-by-columnset" match="p | p-cont |
    heading[not(parent::section or parent::contents) and not(parent::p)] |
    language-desc | country-desc | graphic[not(parent::section or parent::contents)] |
    block-quote | bulleted-list | blank-line |
    bibliography | language-name-index | language-code-index | country-index | table-of-contents" 
    use="sileth:columnset-id(.)"/>

  <!-- TODO: DRY: I would love to be able to share the above match pattern instead of
    duplicating it. -->
  <xsl:key name="narrow-things-by-section" match="p | p-cont |
    heading[not(parent::section or parent::contents) and not(parent::p)] |
    language-desc | country-desc | graphic[not(parent::section or parent::contents)] |
    block-quote | bulleted-list | blank-line |
    bibliography | language-name-index | language-code-index | country-index | table-of-contents" 
    use="sileth:section-id(.)"/>

DRY principal提醒我们,当我们有重复的数据时,我们会遇到保持多个副本同步的问题。事实上,这就发生在我身上,导致了一个花了一段时间才能找到的bug。

因此,我希望能够在两个键之间共享一个单一的、通用的匹配模式。你不能使用一个变量来做这件事。有没有别的办法呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-24 14:03:03

密钥的两级层次结构如何?

像这样..。

代码语言:javascript
复制
<xsl:key name="narrowable-things" match="p | p-cont |
    heading[not(parent::section or parent::contents) and not(parent::p)] |
    language-desc | country-desc | graphic[not(parent::section or parent::contents)] |
    block-quote | bulleted-list | blank-line |
    bibliography | language-name-index | language-code-index | country-index | table-of-contents" 
    use="'universe'"/>

<xsl:key name="narrow-things-by-columnset" match="key('narrowable-things','universe')" use="sileth:columnset-id(.)"/>
<xsl:key name="narrow-things-by-section"   match="key('narrowable-things','universe')" use="sileth:section-id(.)"  />
票数 1
EN

Stack Overflow用户

发布于 2013-06-22 23:33:39

我将使用模式定义一个通用实体,并从两个位置引用它。因此,样式表将开始

代码语言:javascript
复制
<!DOCTYPE xsl:stylesheet [
<!ENTITY match-elements "p | p-cont 
  | heading[not(parent::section or parent::contents) 
      and not(parent::p)] 
  | language-desc | country-desc 
  | graphic[not(parent::section or parent::contents)] 
  | block-quote | bulleted-list | blank-line 
  | bibliography | language-name-index | language-code-index
  | country-index | table-of-contents">
]>
<xsl:stylesheet ...>
...

两个关键的用法是:

代码语言:javascript
复制
<xsl:key name="narrow-things-by-columnset" 
         match="&match-elements;" 
         use="sileth:columnset-id(.)"/>

<!-- DONE: DRY: Isn't is nice to be able to share the above 
     match pattern instead of duplicating it?
     Hooray for general entities! -->

<xsl:key name="narrow-things-by-section" 
         match="&match-elements;"
         use="sileth:section-id(.)"/>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17244436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档