我认为这应该很容易找到,但经过一番搜索后,我发现这可能最好明确定义。
在我的 XSD 中,我定义了一个从字符串派生的枚举。在复杂类型中,我定义了引用此枚举的属性,并具有默认值。
在我的 XSL 中,我希望为未显式设置属性的元素显示该属性的默认值。
The XSD:
<xs:complexType name="foo">
<xs:attribute name="bar" type="responsecodes:barType" default="default"/>
</xs:complexType>
<xs:simpleType name="barType">
<xs:restriction base="xs:string">
<xs:enumeration value="default">
<xs:annotation>
<xs:documentation xml:lang="en-us">Default bar.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="chocolate">
<xs:annotation>
<xs:documentation xml:lang="en-us">A chocolate ...bar</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
The XML:
....
<foo/>
<foo bar="default"/>
<foo bar="chocolate"/>
....
我希望 XSL 是:(或多或少)
<ol>
<xsl:for-each select="/foo">
<li>BarType: '<xsl:value-of select="@bar" />'</li>
</xsl:for-each>
</ol>
现在,当我显示此样式 XML 文件时,对于非指定值,“bar”属性的值为空,而我希望显示(或选择)默认值。
Now:
- 栏类型: ''
- 栏类型:'默认'
- 酒吧类型:“巧克力”
Desired:
- 栏类型:'默认'
- 栏类型:'默认'
- 酒吧类型:“巧克力”
现在这应该很简单,不是吗?
也许我过于笼统,但如果您想从模式加载默认值,您将需要类似以下内容:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
>
<xsl:variable name="schema" select="
document('responsecodes.xsd')
" />
<xsl:variable name="DefaultBar" select="
$schema//xs:complexType[@name='foo']/xs:attribute[@name='bar']/@default
" />
<xsl:template match="foo">
<li>
<xsl:text>BarType: '</xsl:text>
<xsl:choose>
<xsl:when test="@bar">
<xsl:value-of select="@bar" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$DefaultBar" />
</xsl:otherwise>
</xsl:choose>
<xsl:text>'</xsl:text>
</li>
</xsl:template>
</xsl:stylesheet>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)