如何在密钥之间共享匹配模式?

时间:2013-06-21 21:16:58

标签: xslt key xslt-2.0

我有两个具有相同匹配模式的键。模式很长。模式本身并不重要;问题是长期重复:

  <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提醒我们,当我们有重复数据时,我们遇到了保持多个副本同步的问题。事实上,这恰好发生在我身上,造成一个需要一段时间追踪的错误。

所以我希望能够在两个键之间共享一个共同的匹配模式。 AFAIK你不能使用变量做到这一点。还有其他方法吗?

2 个答案:

答案 0 :(得分:4)

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

<!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 ...>
...

两个主要用途是:

<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(.)"/>

答案 1 :(得分:1)

两级密钥层次结构怎么样?

像这样......

<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(.)"  />