使用基于 XSD 的 XSLT 将 XML 转换为 XML [英] Transform XML to XML using XSLT based on XSD

查看:28
本文介绍了使用基于 XSD 的 XSLT 将 XML 转换为 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XSLT/Xalan 将一个 XML 文件转换为另一个.这样做时,我发现在创建 XSLT 样式表时,我对想要生成的目标文件的节点进行了硬编码.这看起来很奇怪.

I am using XSLT/Xalan to transform one XML file into another. 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. This just seemed odd.

有没有办法使用 XSD 以编程方式生成目标文件?我想基本上使用我拥有的 XSD 创建文件的骨架,然后针对源文件运行我的样式表.然后,我可以将我从那里找到的值插入到生成文件中的适当位置.

Is there anyway to programmatically generate the target file using the 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. Then, I can plunk the values I find from there into the appropriate spots in the generated file.

有没有办法做到这一点?或者 XQuery 是否可能提供这样的功能?

Is there any way to do this? Or does possibly XQuery provide functionality like this instead?

推荐答案

您似乎在问如何序列化 DataSet 并使用 XSLT 对其进行转换.如果是这样,您可以这样做:

It sounds like you're asking how to serialize a DataSet and transform it using XSLT. If so, here is how you can do it:

将数据集序列化为 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 并根据需要操作输出.

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.

对数据集应用 XSLT 转换

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

这是我创建的一个简单示例,用于解释您将如何使用 XSL 转换器.我还没有测试过,但应该很接近:

Here's a simple example I created to explain how you would use the XSL transformer. 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(); 

使用 XSLT 将 XML 格式化为 HTML

这是使用 XSLT 将 XML 转换为 HTML 表的示例.它应该很容易采用,因此您可以将它与序列化的 DataSet 一起使用.

Here's an example of using XSLT to transform XML into an HTML table. It should be fairly easy to adopt so you can use it with your serialized DataSet.

假设这是您的数据集,序列化为 XML:

Let's say this is your DataSet, serialized to 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 脚本:

Here's an XSLT script that transforms the DataSet to HTML:

<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> 

这篇关于使用基于 XSD 的 XSLT 将 XML 转换为 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆