2

我认为这应该很容易找到,但经过一番搜索,我发现这可能很好定义清楚。

在我的 XSD 中,我定义了一个从字符串派生的枚举。在我定义的复杂类型和引用此枚举的属性中,具有默认值。

在我的 XSL 中,我希望为未明确设置属性的元素显示此属性的默认值。

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>

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' 属性的值对于未指定的值是空的,而我希望显示(或选择)默认值。

现在:

  1. 栏类型:''
  2. 栏类型:'默认'
  3. BarType: '巧克力'

期望:

  1. 栏类型:'默认'
  2. 栏类型:'默认'
  3. BarType: '巧克力'

现在这应该很简单,不是吗?

4

1 回答 1

1

也许我过于笼统了,但如果你想从模式中加载默认值,你需要一些类似的东西:

<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>
于 2009-12-08T10:00:44.107 回答