我正在使用XSLT/Xalan將一個XML文件轉換爲另一個XML文件。在這樣做的時候,我發現當我創建我的XSLT樣式表時,我對我想生成的目標文件的節點進行了硬編碼。這似乎很奇怪。使用基於XSD的XSLT將XML轉換爲XML
無論如何要以編程方式使用XSD生成目標文件?我想基本上使用我有的XSD創建文件的骨架,然後針對源文件運行我的樣式表。然後,我可以將從那裏找到的值插入生成文件中的適當位置。
有沒有辦法做到這一點?或者可能XQuery提供這樣的功能呢?
我正在使用XSLT/Xalan將一個XML文件轉換爲另一個XML文件。在這樣做的時候,我發現當我創建我的XSLT樣式表時,我對我想生成的目標文件的節點進行了硬編碼。這似乎很奇怪。使用基於XSD的XSLT將XML轉換爲XML
無論如何要以編程方式使用XSD生成目標文件?我想基本上使用我有的XSD創建文件的骨架,然後針對源文件運行我的樣式表。然後,我可以將從那裏找到的值插入生成文件中的適當位置。
有沒有辦法做到這一點?或者可能XQuery提供這樣的功能呢?
聽起來好像您在問如何序列化DataSet並使用XSLT進行轉換。如果是這樣,這裏是你如何能做到這一點:
序列化的DataSet XML
DataTable table = new DataTable();
System.IO.StringWriter writer = new System.IO.StringWriter();
//notice that we're ignoring the schema so we get clean XML back
//you can change the write mode as needed to get your result
table.WriteXml(writer, XmlWriteMode.IgnoreSchema, false);
string dataTableXml = writer.ToString();
至於以可讀格式顯示出來,我會建議傳遞XML到XSL轉換,這然後您可以使用解析XML並根據需要操作輸出。
應用XSLT轉換到DataSet
http://msdn.microsoft.com/en-us/library/8fd7xytc%28v=vs.71%29.aspx#Y289
這裏有一個簡單的例子,我創建的解釋你將如何使用XSL轉換。我沒有測試它,但它應該是相當接近:
DataSet ds = new DataSet();
StringBuilder sbXslOutput = new StringBuilder();
using (XmlWriter xslWriter = XmlWriter.Create(sbXslOutput))
{
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load("transformer.xsl");
XsltArgumentList args = new XsltArgumentList();
transformer.Transform(new XmlDataDocument(ds), args, xslWriter);
}
string dataSetHtml = sbXslOutput.ToString();
使用XSLT
下面是使用XSLT將XML轉換爲HTML表的一個例子格式化XML作爲HTML。它應該相當容易採用,所以你可以在你的序列化數據集中使用它。
比方說,這是你的數據集,序列化到XML:
<RecentMatter>
<UserLogin>PSLTP6\RJK</UserLogin>
<MatterNumber>99999-2302</MatterNumber>
<ClientName>Test Matters</ClientName>
<MatterName>DP Test Matter</MatterName>
<ClientCode>99999</ClientCode>
<OfficeCode/>
<OfficeName/>
<Billable>true</Billable>
<ReferenceId/>
<LastUsed>2011-08-23T23:40:24.13+01:00</LastUsed>
</RecentMatter>
<RecentMatter>
<UserLogin>PSLTP6\RJK</UserLogin>
<MatterNumber>999991.0002</MatterNumber>
<ClientName>Lathe 1</ClientName>
<MatterName>LW Test 2</MatterName>
<ClientCode/>
<OfficeCode/>
<OfficeName/>
<Billable>true</Billable>
<ReferenceId/>
<LastUsed>2011-07-12T16:57:27.173+01:00</LastUsed>
</RecentMatter>
<RecentMatter>
<UserLogin>PSLTP6\RJK</UserLogin>
<MatterNumber>999991-0001</MatterNumber>
<ClientName>Lathe 1</ClientName>
<MatterName>LW Test 1</MatterName>
<ClientCode/>
<OfficeCode/>
<OfficeName/>
<Billable>false</Billable>
<ReferenceId/>
<LastUsed>2011-07-12T01:59:06.887+01:00</LastUsed>
</RecentMatter>
</NewDataSet>
下面是轉換的數據集,以HTML的XSLT腳本:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<table border="1">
<tr>
<th>User Login</th>
<th>Matter Number</th>
...
</tr>
<xsl:for-each select="NewDataSet/RecentMatter">
<tr>
<td>
<xsl:value-of select="UserLogin"/>
</td>
<td>
<xsl:value-of select="MatterNumber"/>
</td>
...
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
使用XSLT 2.0,你可以利用的架構信息(用於源文檔和目標文檔),使系統能夠檢查樣式表是否正確,如果您嘗試訪問輸入或生成對模式無效的輸出,則會給您提供編譯時警告。但是我沒有意識到任何使用該模式來自動創建樣式表的過程的工具。可能有些XSLT編輯工具(IDE)使用模式信息來幫助編輯語法。
這個問題沒有什麼意義,你會*將源節點和目標節點硬編碼到你的樣式表中,因爲你正在定義從一個模式到另一個模式的映射。你怎麼能確定這個編程?讓我知道如果我失去了一些東西。 – IcedDante