我正在尝试创建一个XML模式,它可以捕获如下所示的XML:
<tagname description="simple string type attribute">
false <!-- simple boolean type -->
</tagname>但是我遇到了困难。是否可以定义一个模式来捕获这一点,或者我是否在snipe hunt上
发布于 2010-12-22 07:56:09
这就是你要的
<xs:element name="tagname">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:boolean">
<xs:attribute name="description" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>这是经过验证的示例
<tagname xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:/C:/Untitled2.xsd" description="123">
true
</tagname>发布于 2013-12-04 02:41:57
谢谢,谢谢,谢谢。我已经在这个问题上苦苦挣扎了一段时间了,尽管实际的XML示例非常简单,但如何定义模式并不是那么明显。我最大的问题是如何构造一个JAXB类来处理这个问题。直到我看到您的模式定义并通过xjc运行它,我才知道如何在JAXB中设置它。JAXB java类非常不直观,我永远也猜不到如何设置它。我已经尝试了几种不同的方法来让它工作,但都没有成功。
下面是从您发布的模式生成的JAXB java类的示例。关键是在字段上使用@XmlValue注释(您也可以在字段的getter上使用它,但删除XmlAccessorType注释:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "value" })
@XmlRootElement(name = "tagname")
public class Tagname {
@XmlValue
protected boolean value;
@XmlAttribute(name = "description", required = true)
protected String description;
public boolean isValue() {
return value;
}
public void setValue(boolean value) {
this.value = value;
}
get and set for description omitted.下面是来自给定类的编组JAXB XML文档:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tagname description="The Description">true</tagname>我希望这一补充能帮助其他正在与同样晦涩的问题作斗争的人。
https://stackoverflow.com/questions/4504994
复制相似问题