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

发布于 2024-12-10 10:14:05 字数 238 浏览 0 评论 0原文

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

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

有什么办法可以做到这一点吗?或者 XQuery 是否可能提供类似的功能?

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖竹 2024-12-17 10:14:05

听起来您好像在问如何序列化 DataSet 并使用 XSLT 对其进行转换。如果是这样,您可以这样做:

将数据集序列化为 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 转换应用于数据集

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 将 XML 格式化为 HTML

下面是使用 XSLT 将 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> 

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:

Serialize a DataSet to 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.

Applying an XSLT Transform to a DataSet

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

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

Formatting XML as HTML using XSLT

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.

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>  

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> 
死开点丶别碍眼 2024-12-17 10:14:05

使用 XSLT 2.0,您可以利用模式信息(对于源文档和目标文档)来使系统能够检查样式表的正确性,如果您尝试访问输入或生成对规范无效的输出,则会向您发出编译时警告。架构。但我不知道有任何工具使用该模式来自动化创建样式表的过程。某些 XSLT 编辑工具 (IDE) 可能使用架构信息来帮助进行语法定向编辑。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文