英文:
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
<xsl:template name="cdf:BallotMeasureContest" match="element(*, cdf:BallotMeasureContest)">
and
<xsl:template name="cdf:RetentionContest" match="element(*, cdf:RetentionContest)">
Assuming I have an input of @xsi:type = "cdf:RentionContest"
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
<?xml version="1.0" encoding="UTF-8"?>
<n1:ElectionReport xmlns:n1="http://itl.nist.gov/ns/voting/1500-100/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://itl.nist.gov/ns/voting/1500-100/v2 repro_delete.xsd">
<n1:Election>
<n1:Contest xsi:type="n1:RetentionContest" ObjectId="rc-123">
<n1:CandidateId>rc-123</n1:CandidateId>
</n1:Contest>
</n1:Election>
</n1:ElectionReport>
repro_delete.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns="http://www.w3.org/2005/xpath-functions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cdf="http://itl.nist.gov/ns/voting/1500-100/v2" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:array="http://www.w3.org/2005/xpath-functions/array" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:err="http://www.w3.org/2005/xqt-errors" exclude-result-prefixes="array cdf fn map math xhtml err xs xsi" version="3.0">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:import-schema namespace="http://itl.nist.gov/ns/voting/1500-100/v2" schema-location="repro_delete.xsd"/>
<xsl:template match="/" name="xsl:initial-template">
<xsl:apply-templates/>
</xsl:template>
<xsl:template name="cdf:C" match="element(*, cdf:Contest)">
Contest
</xsl:template>
<xsl:template name="cdf:RC" match="element(*, cdf:RetentionContest)">
Retention
</xsl:template>
<xsl:template name="cdf:BMC" match="element(*, cdf:BallotMeasureContest)">
Ballot Measure Contest
</xsl:template>
</xsl:stylesheet>
repro_delete.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://itl.nist.gov/ns/voting/1500-100/v2"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://itl.nist.gov/ns/voting/1500-100/v2" elementFormDefault="qualified"
version="2.0.3">
<xsd:element name="ElectionReport" type="ElectionReport"/>
<xsd:complexType name="BallotMeasureContest">
<xsd:complexContent>
<xsd:extension base="Contest">
<xsd:sequence>
<xsd:element name="FullText" type="InternationalizedText" minOccurs="0"
> </xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Contest" abstract="true">
<xsd:sequence>
<xsd:element name="SubUnitsReported" type="xsd:integer" minOccurs="0"> </xsd:element>
<xsd:element name="TotalSubUnits" type="xsd:integer" minOccurs="0"> </xsd:element>
</xsd:sequence>
<xsd:attribute name="ObjectId" type="xsd:ID" use="required"/>
</xsd:complexType>
<xsd:complexType name="Election">
<xsd:sequence>
<xsd:element name="Contest" type="Contest" minOccurs="0" maxOccurs="unbounded"
> </xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ElectionReport">
<xsd:sequence>
<xsd:element name="Election" type="Election" minOccurs="0" maxOccurs="unbounded"
> </xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="InternationalizedText">
<xsd:sequence>
<xsd:element name="Text" type="xsd:string" maxOccurs="unbounded"> </xsd:element>
</xsd:sequence>
<xsd:attribute name="Label" type="xsd:string"> </xsd:attribute>
</xsd:complexType>
<xsd:complexType name="RetentionContest">
<xsd:complexContent>
<xsd:extension base="BallotMeasureContest">
<xsd:sequence>
<xsd:element name="CandidateId" type="xsd:IDREF"> </xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论