我有一個文檔需要一些標題具有標準名稱,而另一些則由作者提供。爲了區分差異,我創建了一個屬性「hardTitle」。如果hardTitle屬性有一個值,則標題元素應該顯示hardTitle的值並鎖定它不被編輯。如果hardTitle屬性爲空,那麼作者可以使用他們喜歡的任何標題。將屬性值分配給XML中的元素內容(使用XSD或XSL)
我試過使用枚舉值(下面的代碼),但那隻會告訴我,如果值不正確 - 它不會填充元素中的值,也不會鎖定元素內容被編輯。
我想什麼:
<chapter>
<title hardTitle="Scope">Scope</title> [auto-populated from hardTitle and locked]
...
<title>This Title Can Be Anything</title>
...
<title hardTitle="NewT">NewT</title> [auto-populated from hardTitle and locked]
</chapter>
這裏是我到目前爲止的代碼。我知道xs:restriction將文本限制爲枚舉值...但是我正在尋找一些將強制基於屬性的內容並將其從編輯中鎖定的內容。
.xsd文件片段:
<xs:element name="title" type="editableTitle">
<xs:alternative test="if(@hardTitle)" type="lockedTitle" />
</xs:element>
<xs:complexType name="editableTitle">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="hardTitle" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="lockedTitle">
<xs:simpleContent>
<xs:restriction base="editableTitle">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="@hardTitle" />
</xs:restriction>
</xs:simpleType>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
XSD真的不支持「鎖定「,它會告訴你它是否」無效「。您可能需要針對屬性之間的這種邏輯進行應用程序編程。 –
XSD也不支持修改文檔。 –
感謝您花時間回答我的問題。我剛剛開始使用XML,這已經爲我清楚瞭解我可以做什麼以及不能做什麼用XSD。 –