4

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

  <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 主体提醒我们,当我们有数据重复时,我们会遇到保持多个副本同步的问题。事实上,这只是发生在我身上,导致了一个需要一段时间才能找到的错误。

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

4

2 回答 2

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(.)"/>
于 2013-06-22T15:33:38.503 回答
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(.)"  />
于 2013-06-24T06:03:03.283 回答