XSLT 匹配最具体的子类型

huangapple go评论63阅读模式
英文:

XSLT matching most specific subtype

问题

假设我有一个类型层次结构,其中 RetentionContest BallotMeasureContest Contest(在我的XSD中)。然后我有几个XSLT模板,例如

<xsl:template name="cdf:BallotMeasureContest" match="element(*, cdf:BallotMeasureContest)">

<xsl:template name="cdf:RetentionContest" match="element(*, cdf:RetentionContest)">

假设我的输入是 @xsi:type = "cdf:RentionContest",并且我正在使用一个Schema-aware处理器,那么哪个模板会被调用?我如何确保调用最具体子类型的模板?(注意:我尝试了优先级,但没有太大成功)。Altova 和 Saxon-EE 似乎只是调用最后一个适用的模板。

英文:

Suppose I have a type hierarchy such that RetentionContest is-a BallotMeasureContest is-a Contest (in my XSD). Then I have several XSLT templates such as

&lt;xsl:template name=&quot;cdf:BallotMeasureContest&quot; match=&quot;element(*, cdf:BallotMeasureContest)&quot;&gt;

and

&lt;xsl:template name=&quot;cdf:RetentionContest&quot; match=&quot;element(*, cdf:RetentionContest)&quot;&gt;

Assuming I have an input of @xsi:type = &quot;cdf:RentionContest&quot; and I am using a Schema-aware processor, which template gets called? How can I make sure the template for the most specific subtype gets called? (NB: I tried priority without much success). Altova and Saxon-EE seem to just call the last applicable one.

repro_delete.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;n1:ElectionReport xmlns:n1=&quot;http://itl.nist.gov/ns/voting/1500-100/v2&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://itl.nist.gov/ns/voting/1500-100/v2 repro_delete.xsd&quot;&gt;
	&lt;n1:Election&gt;
		&lt;n1:Contest xsi:type=&quot;n1:RetentionContest&quot; ObjectId=&quot;rc-123&quot;&gt;
			&lt;n1:CandidateId&gt;rc-123&lt;/n1:CandidateId&gt;
		&lt;/n1:Contest&gt;
	&lt;/n1:Election&gt;
&lt;/n1:ElectionReport&gt;

repro_delete.xsl

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet xmlns=&quot;http://www.w3.org/2005/xpath-functions&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:cdf=&quot;http://itl.nist.gov/ns/voting/1500-100/v2&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:fn=&quot;http://www.w3.org/2005/xpath-functions&quot; xmlns:math=&quot;http://www.w3.org/2005/xpath-functions/math&quot; xmlns:array=&quot;http://www.w3.org/2005/xpath-functions/array&quot; xmlns:map=&quot;http://www.w3.org/2005/xpath-functions/map&quot; xmlns:xhtml=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:err=&quot;http://www.w3.org/2005/xqt-errors&quot; exclude-result-prefixes=&quot;array cdf fn map math xhtml err xs xsi&quot; version=&quot;3.0&quot;&gt;
	&lt;xsl:output method=&quot;text&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/&gt;
	&lt;xsl:import-schema namespace=&quot;http://itl.nist.gov/ns/voting/1500-100/v2&quot; schema-location=&quot;repro_delete.xsd&quot;/&gt;
	&lt;xsl:template match=&quot;/&quot; name=&quot;xsl:initial-template&quot;&gt;
		&lt;xsl:apply-templates/&gt;
	&lt;/xsl:template&gt;
		&lt;xsl:template name=&quot;cdf:C&quot; match=&quot;element(*, cdf:Contest)&quot;&gt;
Contest
	&lt;/xsl:template&gt;

	&lt;xsl:template name=&quot;cdf:RC&quot; match=&quot;element(*, cdf:RetentionContest)&quot;&gt;
Retention
	&lt;/xsl:template&gt;
	&lt;xsl:template name=&quot;cdf:BMC&quot; match=&quot;element(*, cdf:BallotMeasureContest)&quot;&gt;
Ballot Measure Contest
	&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

repro_delete.xsd

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsd:schema xmlns=&quot;http://itl.nist.gov/ns/voting/1500-100/v2&quot;
	xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
	targetNamespace=&quot;http://itl.nist.gov/ns/voting/1500-100/v2&quot; elementFormDefault=&quot;qualified&quot;
	version=&quot;2.0.3&quot;&gt;
	&lt;xsd:element name=&quot;ElectionReport&quot; type=&quot;ElectionReport&quot;/&gt;
	&lt;xsd:complexType name=&quot;BallotMeasureContest&quot;&gt;
		&lt;xsd:complexContent&gt;
			&lt;xsd:extension base=&quot;Contest&quot;&gt;
				&lt;xsd:sequence&gt;
					&lt;xsd:element name=&quot;FullText&quot; type=&quot;InternationalizedText&quot; minOccurs=&quot;0&quot;
					&gt; &lt;/xsd:element&gt;
				&lt;/xsd:sequence&gt;
			&lt;/xsd:extension&gt;
		&lt;/xsd:complexContent&gt;
	&lt;/xsd:complexType&gt;
	&lt;xsd:complexType name=&quot;Contest&quot; abstract=&quot;true&quot;&gt;
		&lt;xsd:sequence&gt;
			&lt;xsd:element name=&quot;SubUnitsReported&quot; type=&quot;xsd:integer&quot; minOccurs=&quot;0&quot;&gt; &lt;/xsd:element&gt;
			&lt;xsd:element name=&quot;TotalSubUnits&quot; type=&quot;xsd:integer&quot; minOccurs=&quot;0&quot;&gt; &lt;/xsd:element&gt;
		&lt;/xsd:sequence&gt;
		&lt;xsd:attribute name=&quot;ObjectId&quot; type=&quot;xsd:ID&quot; use=&quot;required&quot;/&gt;
	&lt;/xsd:complexType&gt;
	&lt;xsd:complexType name=&quot;Election&quot;&gt;
		&lt;xsd:sequence&gt;
			&lt;xsd:element name=&quot;Contest&quot; type=&quot;Contest&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;
			&gt; &lt;/xsd:element&gt;
		&lt;/xsd:sequence&gt;
	&lt;/xsd:complexType&gt;
	&lt;xsd:complexType name=&quot;ElectionReport&quot;&gt;
		&lt;xsd:sequence&gt;
			&lt;xsd:element name=&quot;Election&quot; type=&quot;Election&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;
			&gt; &lt;/xsd:element&gt;
		&lt;/xsd:sequence&gt;
	&lt;/xsd:complexType&gt;
	&lt;xsd:complexType name=&quot;InternationalizedText&quot;&gt;
		&lt;xsd:sequence&gt;
			&lt;xsd:element name=&quot;Text&quot; type=&quot;xsd:string&quot; maxOccurs=&quot;unbounded&quot;&gt; &lt;/xsd:element&gt;
		&lt;/xsd:sequence&gt;
		&lt;xsd:attribute name=&quot;Label&quot; type=&quot;xsd:string&quot;&gt; &lt;/xsd:attribute&gt;
	&lt;/xsd:complexType&gt;
	&lt;xsd:complexType name=&quot;RetentionContest&quot;&gt;
		&lt;xsd:complexContent&gt;
			&lt;xsd:extension base=&quot;BallotMeasureContest&quot;&gt;
				&lt;xsd:sequence&gt;
					&lt;xsd:element name=&quot;CandidateId&quot; type=&quot;xsd:IDREF&quot;&gt; &lt;/xsd:element&gt;
				&lt;/xsd:sequence&gt;
			&lt;/xsd:extension&gt;
		&lt;/xsd:complexContent&gt;
	&lt;/xsd:complexType&gt;
&lt;/xsd:schema&gt;

答案1

得分: 0

分配默认优先级给不同的XSLT模式的规则相当粗糙;它们并不总是具有这样的效果,即如果模式P匹配模式Q匹配的节点子集,那么P会优先于Q。特别是,所有形式为element(*, T)的模式具有相同的默认优先级,而不考虑模式的模式类型。

您需要通过使用priority属性来分配显式优先级来处理这个问题。

英文:

The rules for allocating a default priority to different XSLT patterns are somewhat crude; they don't always have the effect that if pattern P matches a subset of the nodes matched by pattern Q, then P is chosen in preference to Q. In particular, all patterns of the form element(*, T) have the same default priority regardless of the schema type.

You will need to handle this by allocating explicit priorities using the priority attribute.

huangapple
  • 本文由 发表于 2023年3月3日 23:18:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定