How can I share matching patterns between keys?

I have two keys with the same matching pattern. The pattern is long. The picture itself does not matter; the problem is long duplication:

<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 main reminds us that when duplicating data there are problems with synchronizing multiple copies. In fact, this happened to me, as a result of which the error was postponed for tracking.

Therefore, I would like to be able to use one common pattern of correspondence between two keys. AFAIK you cannot do this with a variable. Is there any other way to do this?

+4
source share
2 answers

What about a two-level key hierarchy?

like this...

 <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
source

I would define a common object with a template and access it from two places. So the stylesheet starts

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

And using two keys will be:

 <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
source

Source: https://habr.com/ru/post/1487565/


All Articles