我了解到,xsl:attribute-set的存在是为了允许将一组XML属性分组在一个名称下,然后可以轻松地在稍后的某个日期将其应用于几个类似的元素。
我理解名称空间不是属性,不能使用它来设置。
然而,在Saxon9.8EE中,我注意到这是可行的,我想知道这是否安全:
<xsl:attribute-set name="swbml.ir" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:attribute name="version">4-2</xsl:attribute>
<xsl:attribute name="xsi:schemaLocation">http://www.fpml.org/2005/FpML-4-2 /path/to/swbml-ird-main-4-2.xsd</xsl:attribute>
</xsl:attribute-set>通过将xsi命名空间添加到xsl:attribute-set本身,它将此命名空间应用于使用swbml.ir属性集的任何元素。
(当然必须这样做,因为其中一个属性位于xsi命名空间中)
所以这个:
<SWBML xmlns="http://www.fpml.org/2005/FpML-4-2" xsl:use-attribute-sets="swbml.ir">在以下方面的成果:
<SWBML xmlns="http://www.fpml.org/2005/FpML-4-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="4-2"
xsi:schemaLocation="http://www.fpml.org/2005/FpML-4-2 /path/to/swbml-ird-main-4-2.xsd">这正是我想要的。但我觉得我可能是在扩展属性集的预期用例吗?
具体来说,如果我想更进一步添加xmlns="http://www.fpml.org/2005/FpML-4-2",如下所示:
<xsl:attribute-set name="swbml.ir" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.fpml.org/2005/FpML-4-2">默认的xmlns不应用于<SWBML> --这正是我所期望的。
那么-属性集将添加任何命名空间以限定该集包含的任何属性,但不会添加任何其他命名空间吗?还是我误入歧途了?
发布于 2018-04-17 23:25:07
您的理解基本上是正确的,因为如果存在绑定到名称空间的内容,并且将其包含在输出中,那么命名空间就会出现。但是,您碰巧在属性集上声明了它这一事实并不重要。它可以在样式表的其他地方声明,比如在xsl:样式表元素上声明,在范围内,并在属性集中引用。
在您提出的示例的基础上,您可以将xsi命名空间前缀的声明从xsl:attribute-set移到xsl:stylesheet元素,如果属性集应用于该元素,它仍然会出现在您的输出中:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="xsi">
<xsl:output method="xml"/>
<xsl:attribute-set name="swbml.ir">
<xsl:attribute name="version">4-2</xsl:attribute>
<xsl:attribute name="xsi:schemaLocation">http://www.fpml.org/2005/FpML-4-2 /path/to/swbml-ird-main-4-2.xsd</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="/">
<SWBML xmlns="http://www.fpml.org/2005/FpML-4-2" xsl:use-attribute-sets="swbml.ir"/>
</xsl:template>
</xsl:stylesheet>如果属性集不应用于内容,则它不会出现在输出中:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="xsi">
<xsl:output method="xml"/>
<xsl:attribute-set name="swbml.ir">
<xsl:attribute name="version">4-2</xsl:attribute>
<xsl:attribute name="xsi:schemaLocation">http://www.fpml.org/2005/FpML-4-2 /path/to/swbml-ird-main-4-2.xsd</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="/">
<SWBML xmlns="http://www.fpml.org/2005/FpML-4-2" />
</xsl:template>
</xsl:stylesheet>请注意,我在这两个示例中都使用了exclude-result-prefixes,以确保如果未使用,则从输出中删除xsi名称。否则,即使没有将其应用于任何内容,也可能会出现在输出中的范围内命名空间。
发布于 2018-04-18 07:33:06
是的,这是可行的:正如@MadsHansen所指出的,当您使用<xsl:attribute name="p:u"/>时,唯一真正重要的是前缀p是在某个地方声明的--在xsl:attribute元素本身上,或者在它的一个祖先上。如果在xsl:attribute-set本身的级别上声明它是方便的,那么就这样做吧。
这里需要注意的一点是,这不适用于QName值属性。如果你想做
<xsl:attribute name="xsi:type">xs:date</xsl:attribute>然后,只需让前缀xsi在xsl:attribute指令范围内声明,就可以在结果文档中声明前缀,但是对于xs前缀,您需要更加努力地工作(因为XSLT处理器不知道属性值xs:date是QName)。在这种情况下,您需要显式地确保结果树中的某些包含元素声明xs命名空间。
https://stackoverflow.com/questions/49886441
复制相似问题