2015-10-12 40 views
0

我一直在這個項目上工作幾天。該任務讓我創建一個基於XML文件並基於DTD文件的XSL樣式表。一旦我最終獲得了XLT文件來驗證(我希望我做的一切都正確),我試了幾個小時才用XML生成結果文檔,但沒有運氣。我下載了教科書中推薦的Saxon,但即使我提取了文件等,但仍然收到錯誤消息。有沒有更簡單的方法來生成結果XML文檔?我也需要確保驗證。當Saxon不適合我時,如何生成結果文檔?

這裏是源代碼:

DTD file 
 

 

 
<!ELEMENT customers (customer)*> 
 

 
<!ELEMENT customer (order)> 
 
<!ATTLIST customer id CDATA #IMPLIED> 
 

 
<!ELEMENT order (qty, date, amount)> 
 
<!ATTLIST order orderid CDATA #IMPLIED> 
 

 
<!ELEMENT qty (#PCDATA)> 
 
<!ELEMENT date (#PCDATA)> 
 
<!ELEMENT amount (#PCDATA)> 
 

 

 

 
​
<?xml version="1.0" encoding="UTF-8" ?> 
 

 
<xsl:stylesheet version="1.0" 
 
\t xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
 
\t <xsl:output method="xml" 
 
\t \t doctype-system="customers.dtd" 
 
\t \t encoding="UTF-8" 
 
\t \t indent="yes" /> 
 
\t \t 
 
<xsl:template match="/"> 
 
    <xsl:comment> 
 
\t Author: Paulina Crawford 
 
\t Date: 10/10/2015 
 
\t </xsl:comment> 
 
\t 
 
<xsl:element name="customers"> 
 
    <xsl:apply-templates 
 
    \t select="orders/order"> 
 
     <xsl:sort select="custid" /> 
 
    </xsl:apply-templates> 
 
</xsl:element> 
 
<xsl:apply-templates select="order" /> 
 
<xsl:element name="customer"> 
 
\t <xsl:attribute name="id"> 
 
\t <xsl:value-of select="custid" /> 
 
\t </xsl:attribute> 
 
\t <xsl:element name="order"> 
 
\t <xsl:value-of select="@orderid/id" /> 
 
\t <xsl:value-of select="@qty" /> 
 
\t <xsl:value-of select="@date" /> 
 
\t <xsl:value-of select="@amount" /> 
 
\t </xsl:element> 
 
<xsl:value-of select="custid" /> 
 
</xsl:element> 
 
</xsl:template> 
 
</xsl:stylesheet>​
<?xml version="1.0" encoding="UTF-8" ?> 
 

 

 
<?xml-stylesheet type="text/xsl" href="clist.xsl" ?> 
 

 
<orders> 
 
    <order id="OR3124" qty="1" custid="CUST204"> 
 
     <date>5/1/2017</date> 
 
     <amount>$108.24</amount> 
 
    </order> 
 
    <order id="OR3125" qty="2" custid="CUST117"> 
 
     <date>5/1/2017</date> 
 
     <amount>$78.21</amount> 
 
    </order> 
 
    <order id="OR3126" qty="1" custid="CUST311"> 
 
     <date>5/1/2017</date> 
 
     <amount>$45.93</amount> 
 
    </order> 
 
    <order id="OR3127" qty="4" custid="CUST091"> 
 
     <date>5/2/2017</date> 
 
     <amount>$68.21</amount> 
 
    </order> 
 
    <order id="OR3128" qty="1" custid="CUST137"> 
 
     <date>5/2/2017</date> 
 
     <amount>$117.24</amount> 
 
    </order> 
 
    <order id="OR3129" qty="1" custid="CUST128"> 
 
     <date>5/3/2017</date> 
 
     <amount>$75.68</amount> 
 
    </order> 
 
    <order id="OR3130" qty="2" custid="CUST083"> 
 
     <date>5/3/2017</date> 
 
     <amount>$58.93</amount> 
 
    </order> 
 
    <order id="OR3131" qty="1" custid="CUST304"> 
 
     <date>5/3/2017</date> 
 
     <amount>$112.25</amount> 
 
    </order> 
 
</orders>​

如果任何人都可以請提供任何幫助,我將永遠感激......

更新的代碼:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
 

 
<xsl:stylesheet version="1.0" 
 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
 
<xsl:output method="xml" 
 
\t doctype-system="customers.dtd" 
 
\t encoding="ISO-8859-1" 
 
\t indent="yes" /> 
 
\t 
 
<xsl:template match="/"> 
 

 
    <xsl:element name="customers"> 
 
     <xsl:apply-templates 
 
    \t  select="orders/order"> 
 
      <xsl:sort select="custid" /> 
 
      </xsl:apply-templates> 
 
      </xsl:element> 
 

 
      <xsl:apply-templates select="order"> 
 
      <xsl:element name="customer"> 
 
     <xsl:attribute name="id"> 
 
      <xsl:value-of select="@custid" /> 
 
      </xsl:attribute> 
 
     <xsl:element name="order"> 
 
     <xsl:attribute name="orderid"> 
 
      <xsl:value-of select="@id" /> 
 
     <xsl:element name="qty"> 
 
      <xsl:value-of select="@qty" /> 
 
     <xsl:element name="date"> 
 
     <xsl:value-of select="date" /> 
 
    <xsl:element name="amount"> 
 
     <xsl:value-of select="amount" /> 
 
    </xsl:element> 
 
     <xsl:value-of select="custid" /> 
 
    </xsl:templates> 
 
    </xsl:stylesheet> 
 

+0

如果您不告訴我們錯誤消息是什麼,告訴我們您收到錯誤消息是毫無意義的。此外,您提交的樣式表代碼中沒有調用xsl:result-document。所以我們完全不知道你做錯了什麼。 –

回答

1

正如我的評論中所提到的,我們無法看到您對xsl:result-document的調用錯誤,而沒有看到該代碼或看到錯誤消息。以下是您向我們顯示的代碼中存在錯誤的一些內容:

您將(兩次)作爲元素名稱引用custid,但沒有稱爲custid的元素。也許你打算使用該名稱的屬性,這將是@custid。

您引用屬性@date和@amount,但這些是您的源中的元素,而不是屬性。您還可以參考@ orderid/id,它不能在任何XML文檔中存在:屬性節點沒有元素作爲子元素。最後,所有的代碼都在「根」模板中(匹配=「/」),但是如果上下文項是「order」元素,那麼大多數路徑表達式只有意義。

坦率地說,我認爲你會以錯誤的方式去完成這項任務。你說你已經做了好幾天了。我懷疑你這段時間一直在努力讓代碼工作,而你應該花時間坐在舒適的椅子上閱讀一本書。在閱讀任何合理的XSLT教科書兩天後,理想情況下執行一些工作示例,您在一小時內編寫此樣式表就會遇到一些麻煩。

+0

謝謝你的誠實話語。我真的很感激我知道我的代碼有什麼問題。我其實在XML和XSL方面非常新,但我接受了你的建議。老實說,儘管我有HTML背景,但我仍然無法理解它。我將當前的代碼添加到OP,並且我沒有收到錯誤消息,這就是爲什麼我認爲它已經完成了。該作業還告訴我按照custid排序... – Pau808

+0

與許多現代語言相比,XSLT不擅長靜態檢測錯誤。很多時候,如果你的代碼錯了,你只會得到錯誤的輸出。這實際上是原始設計理念的一部分,與HTML共享,並被認爲適用於瀏覽器世界:垃圾進入,出了什麼東西。靜態錯誤檢測在後來的XSLT版本中得到了改進,如果您使用支持架構的XSLT,但仍然遠非完美。 –