使用XSL显示XSD定义的属性默认值 [英] Display XSD-defined default value of attribute using XSL

查看:211
本文介绍了使用XSL显示XSD定义的属性默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这应该很容易找到,但是经过一番搜索后,我发现可以很好地定义它。

I thought this ought to be simple to find, yet after some searching I found this might be nice to define clearly.

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

In my XSD I've defined an enum, derived from string. In a complex type I've defined and attribute that refers to this enum, with a default value.

在我的XSL中,我希望显示该属性的默认值,例如

In my XSL I wish to display the default value of this attribute for elements whose attribute is not explicitly set.

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为:(或多或少)

I'd expect the XSL to be: (more or less)

<ol>
<xsl:for-each select="/foo">
    <li>BarType: '<xsl:value-of select="@bar" />'</li>
</xsl:for-each>
</ol>

现在,当我显示此样式XML文件时,'bar'属性的值对于非指定值,而我希望显示(或选择)默认值。

Now when I display this style XML file, the value of the 'bar' attribute is empty for the non-specified value, while I'd wish to display (or select on) the default value.

现在:


  1. BarType:''

  2. BarType:'default'

  3. BarType:'chocolate'

所需:


  1. BarType:默认

  2. BarType:'default'

  3. BarType:'chocolate'

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

Now this ought to be quite simple, no?

推荐答案

也许我过于概括了,但是如果要加载模式的默认值,您将需要以下内容:

Maybe I'm overgeneralizing, but if you want to load the default from the schema, you would need something along the lines of this:

<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>

这篇关于使用XSL显示XSD定义的属性默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆