問題參考在XML文件中配置屬性:有沒有一種方法(使用類似tns:
)建立一個XML文件.xsd
配置來引用在XML文件配置屬性的列表在運行時?XSD配置在運行時
這是我的情況:
我有一個.xsd
模式文件,Labels.xsd
爲XML文件Labels.xml
。
Labels.xsd
包含以下內容:
<complexType name="LabelList">
<sequence>
<element name="label" type="tns:MyLabel" maxOccurs="unbounded"
minOccurs="1"/>
</sequence>
</complexType>
<complexType name="MyLabel">
<attribute name="labelName" type="token" use="required"/>
<!-- There are other attributes and sequences in MyLabel -->
</complexType>
用戶可以添加到Labels.xml
文件,並添加自己的自定義標籤,並更改名稱,而我的Java應用程序運行。這些更改在運行時收集。
我有另一個文件,MyMainTable.xml
使用Tables.xsd
架構。該文件使用上述Labels.xml
文件中的標籤創建一個表格。該架構與Labels.xml
文件的架構位於不同的架構文件中。
的Tables.xml
文件包含以下內容:Labels.xml
的
<complexType name="MyMainTable">
<sequence>
<element name="table" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="tableElement" maxOccurs="unbounded">
<complexType>
<attribute name="name" type="string" use="required">
<annotation>
<documentation>
This should be one of the types of
MyLabel.label.labelName that are configured in the
LabelList sequence
Users can change the names of and add to the
types in MyLabel.labelName xml file.
</documentation>
</annotation>
</attribute>
</complexType>
</element>
</sequence>
<!-- There are other attributes in this type, but they are not relevant -->
</complexType>
</element>
</sequence>
</complexType>
例子:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Labels xmlns="<my schema locaiton>">
<FavoriteLabels>
<label labelName="LabelA">
<!-- other attributes such as color -->
</label>
<label labelName="LabelB">
<!-- other attributes such as color -->
</label>
<label labelName="LabelC">
<!-- other attributes such as color -->
</label>
</FavoriteLabels>
<OtherLabels>
<label labelName="OtherA">
<!-- other attributes such as color -->
</label>
<label labelName="OtherB">
<!-- other attributes such as color -->
</label>
<label labelName="OtherC">
<!-- other attributes such as color -->
</label>
</OtherLabels>
</Labels>
在tableElement名稱可以是在FavoriteLabels列表或OtherLabels列表
目前在構建表時,我在Java代碼中執行檢查,但是我希望在解析XML文件時完成檢查。與.xsd
文件中配置的Enum
的檢查方式類似。
有沒有在.xsd
配置的方式,以確保tableElement
的name
屬性包含在用戶已經在Labels.xml
配置文件在運行時的標籤列表?
您可以顯示一個Labels.xml文件的簡短示例嗎? – helderdarocha
@helderdarocha我添加了一個例子。這一切都不是從我的代碼中完全剪切和粘貼的,因爲還有其他的細節使得代碼更加複雜一些,與問題無關,但是xml文件基本上是從我的xml文件中複製並粘貼的,只是名稱改變和一些項目註釋掉。 – mdewitt
這似乎是一個可以通過key/keyref解決的問題。你嘗試過使用它嗎?我有一個例子,我可以嘗試適應你的問題。 – helderdarocha