2011-05-14 23 views
1

XML新手,編寫了一個XML文檔並使用Visual Studio自動生成了一個Schema。我最初開始寫自己的模式。我的問題是,我可以實施哪些缺陷或可能的改進?我有類型,要求和事件驗證,只是好奇聽到你有經驗的人說。XML Schema/XML文檔結構 - 正確書寫

XML

<?xml version="1.0" encoding="utf-8" ?> 
<university> 
<lesson id="ms434"> 
    <subject>Biology</subject> 
    <maintopic name="Human Biology"> 
    <subtopic>Enlarge Hearts</subtopic> 
    <subtopic>Heart Valves</subtopic> 
    </maintopic> 
    <content> 
    <sentance>Very long sentance one</sentance> 
    <sentance>Very long sentance two</sentance> 
    <sentance>Very long sentance three</sentance> 
    </content> 
</lesson> 
</university> 

模式

<?xml version="1.0" encoding="utf-8"?> 

<xsl:template match="@* | node()"> 
    <html> 
    <body> 
     <h1>Professional Training Facilities</h1> 
     <p> 
     <strong>University: </strong> 
     <xsl:for-each select="university/lesson">   
     <xsl:value-of select="subject"/> 
     </p> 
     <br/> 
     <p> 
     <strong>Main Topic: </strong> 
     <xsl:value-of select="maintopic=name"/> 
     </p> 
     <br/> 
     <p> 
     <strong>Sub Topics: </strong> 
     <xsl:for-each select="maintopic"> 
      <p> 
      <xsl:value-of select="subtopic"/> 
      </p> 
     </xsl:for-each> 
     </p> 
     <p></p> 
     <strong>Content:</strong> 
     <xsl:for-each select="content"> 
     <p> 
      <xsl:value-of select="sentance"/> 
     </p> 
     </xsl:for-each> 
     </xsl:for-each> 
     <br/> 
    </body> 
    </html> 
</xsl:template> 

當我幾乎手動完成Schema時,我確信我的代碼少得多,是否自動生成過多?

編輯:第一個語句的foreach錯誤,工作修復,這不是問題btw。

回答

0

模式是一類文檔的描述。從單個文檔生成的任何模式都是猜測。例如,如果所有課程元素的長度屬性都是整數,則該工具可能會猜測它始終是一個整數,併爲其提供一種xs:integer。但是你可能想要更精確一些,並且使得這個類型是一個30到60範圍內的整數。或者這個工具可能比你想要的更具限制性:或許它猜測ID總是5個字符長,當這只是一個意外的樣本數據。因此,無論何時您使用這樣的模式生成工具,您都需要檢查輸出並將其更改爲描述一類文檔,而不僅僅是您的示例。

我不知道VS工具,但很多這樣的工具都可以選擇生成模式的樣式,例如,局部元素聲明與全局元素聲明。不同的輸出可能是等價的,但其中一些可能允許更多地重用組件或更易於修改。值得嘗試不同的選擇。