简体   繁体   English

使用基于XSD的XSLT将XML转换为XML

[英]Transform XML to XML using XSLT based on XSD

I am using XSLT/Xalan to transform one XML file into another. 我正在使用XSLT / Xalan将一个XML文件转换为另一个。 In doing so, I found that when I was creating my XSLT stylesheet, I was hardcoding the nodes of the target file I wanted generated. 这样,我发现在创建XSLT样式表时,我正在对要生成的目标文件的节点进行硬编码。 This just seemed odd. 这似乎很奇怪。

Is there anyway to programmatically generate the target file using the XSD? 无论如何,是否可以使用XSD以编程方式生成目标文件? I want to basically create the skeleton of the file using the XSD I have and then run my stylesheet against the source file. 我想基本上使用现有的XSD创建文件的框架,然后对源文件运行样式表。 Then, I can plunk the values I find from there into the appropriate spots in the generated file. 然后,我可以将从那里找到的值放入生成的文件中的相应位置。

Is there any way to do this? 有什么办法吗? Or does possibly XQuery provide functionality like this instead? 还是XQuery可能提供了这样的功能?

It sounds like you're asking how to serialize a DataSet and transform it using XSLT. 听起来您正在询问如何序列化DataSet并使用XSLT对其进行转换。 If so, here is how you can do it: 如果是这样,请按照以下步骤操作:

Serialize a DataSet to XML 将数据集序列化为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(); 

As for displaying it in a readable format, I would suggest passing the XML into an XSL transformer, which you can then use to parse the XML and manipulate the output as needed. 至于以可读格式显示它,我建议将XML传递给XSL转换器,然后您可以使用它解析XML并根据需要处理输出。

Applying an XSLT Transform to a DataSet 将XSLT转换应用于数据集

http://msdn.microsoft.com/en-us/library/8fd7xytc%28v=vs.71%29.aspx#Y289 http://msdn.microsoft.com/zh-cn/library/8fd7xytc%28v=vs.71%29.aspx#Y289

Here's a simple example I created to explain how you would use the XSL transformer. 这是我创建的一个简单示例,用于解释如何使用XSL转换器。 I haven't tested it, but it should be pretty close: 我还没有测试过,但是应该非常接近:

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(); 

Formatting XML as HTML using XSLT 使用XSLT将XML格式化为HTML

Here's an example of using XSLT to transform XML into an HTML table. 这是一个使用XSLT将XML转换为HTML表的示例。 It should be fairly easy to adopt so you can use it with your serialized DataSet. 它应该很容易采用,因此可以将其与序列化的DataSet一起使用。

Let's say this is your DataSet, serialized to XML: 假设这是序列化为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>  

Here's an XSLT script that transforms the DataSet to HTML: 这是一个将数据集转换为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> 

With XSLT 2.0 you can take advantage of schema information (for both your source and target documents) to enable the system to check your stylesheet for correctness, giving you compile time warnings if you try to access input or generate output that would be invalid against the schema. 借助XSLT 2.0,您可以利用模式信息(针对源文档和目标文档)来使系统能够检查样式表的正确性,如果您尝试访问输入或生成对代码无效的输出,则会向您发出编译时警告。模式。 But I'm not aware of any tools that use the schema to automate the process of creating the stylesheet. 但我不知道有任何使用该架构的工具来自动创建样式表的过程。 It might be that some of the XSLT editing tools (IDEs) use schema information to help with syntax-directed editing. 某些XSLT编辑工具(IDE)可能使用模式信息来帮助进行语法指导的编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM