xml到xsd的转换问题 [英] xml to xsd conversion problem

查看:109
本文介绍了xml到xsd的转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用xsd.exe工具为以下文件创建XML Schema(XSD)

 <  时间 >  
< Hours_List >
< 数字 > 12 < / Number >
< 名称 > 08:00 < /姓名 >
< ; 姓名 > 09:00 < span class =code-keyword>< / Name >
< 名称 > 10: 00 < /姓名 >
< 名称 > 11:00 < /姓名 >
< 名称 > 12:00 < /姓名 >
< 名称 > 13:00 < / Name >
< 名称 > 14:00 < / Name >
< 名称 > 15:00 < / Name >
< 姓名 < span class =code-keyword>> 16:00 < /姓名 >
< 名称 > 17:00 < / Name >
< 名称 > 18:00 < /姓名 >
< 名称 > 19:00 < /姓名 >
< / Hours_List >

< < span class =code-leadattribute> Days_List
>
< Number > 5 < / Number >
< < span class =code-leadattribute>名称 > 星期一< /姓名 >
< < span class =code-leadattribute>名称 > 星期二< /姓名 >
< < span class =code-leadattribute>名称 > 星期三< / Name >
< 名称 > 星期四< /姓名 >
< 名称 > 星期五< /姓名 >
< / Days_List >
< / time >

,或直接在解决方案资源管理器中拖动xsd文件并编写此代码

时间t = 时间(); 
t.ReadXml( @ E:\ Time_check.xml);





使用此xsd架构的数据集时出现错误消息

同一个表'名称'不能是两个嵌套关系中的子表。



如何解决这个问题?

我想要处理这个架构而不用

将name元素更改为hourName和dayName,例如将number元素更改为属性,因为另一个程序(非开源)采用生成的XML文件来处理。

解决方案

您可以使用XSLT重命名节点,然后将其读入,然后在写入输出时将其重命名。



 <?  xml     version   =  1.0    encoding   =  utf-8  >  
< xsl:stylesheet < span class =code-attribute> version = 1.0 xmlns:xsl = http://www.w3.org/1999/XSL/Transform

x mlns:msxsl = urn:schemas-microsoft-com:xslt exclude-result-prefixes = msxsl >
< xsl:output method = xml 缩进 = / >

< xsl:template 匹配 = * >
< xsl:param name = parentElm >
< xsl:value-of 选择 = name(..) / >
< / xsl:param >
< xsl:choose >
< xsl:when test = local-name()='姓名' >
< xsl:element name = {concat('Name',


parentElm)} >
< xsl:apply-templates 选择 = @ * | node() / >
< / xsl:element >
< / xsl:when >
< xsl:否则 >
< xsl:element name = {local-name()} >
< xsl:copy-of 选择 = @ * / >
< xsl:apply-templates 选择 = @ * | node() / >
< / xsl:element >
< / xsl:否则 >
< / xsl:choose < span class =code-keyword>>
< / xsl:template >
< / xsl:stylesheet >





我参加了XSLT例如: http:// stackoverflow .com / questions / 397426 / xsd-class-generation-nested-tables-problem [ ^ ]


using xsd.exe tool to create XML Schema (XSD) for the following file

<time>
<Hours_List>
    <Number>12</Number>
    <Name>08:00</Name>
    <Name>09:00</Name>
    <Name>10:00</Name>
    <Name>11:00</Name>
    <Name>12:00</Name>
    <Name>13:00</Name>
    <Name>14:00</Name>
    <Name>15:00</Name>
    <Name>16:00</Name>
    <Name>17:00</Name>
    <Name>18:00</Name>
    <Name>19:00</Name>
</Hours_List>

<Days_List>
    <Number>5</Number>
    <Name>Monday</Name>
    <Name>Tuesday</Name>
    <Name>Wednesday</Name>
    <Name>Thursday</Name>
    <Name>Friday</Name>
</Days_List>
</time>


by xsd.exe and \dataset option , or drag xsd file directly in solution explorer and write this code

Time t = new Time();
 t.ReadXml(@"E:\Time_check.xml");



error meassage appear when using dataset for this xsd schema
The same table 'Name' cannot be the child table in two nested relations.

how can i solve this problem ?
I want to handle this schema without
change "name" element to "hourName" and "dayName" for example or change "number" element to attribute,since another program (not open source) take generated XML file to deal with.

解决方案

You could rename the nodes with XSLT and then read it in, and then rename them back when writing the output.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

  <xsl:template match="*">
    <xsl:param name="parentElm">
      <xsl:value-of select="name(..)" />
    </xsl:param>
    <xsl:choose>
      <xsl:when test="local-name() = 'Name'">
        <xsl:element name="{concat('Name',


parentElm)}"> <xsl:apply-templates select="@* | node()" /> </xsl:element> </xsl:when> <xsl:otherwise> <xsl:element name="{local-name()}"> <xsl:copy-of select="@*" /> <xsl:apply-templates select="@* | node()" /> </xsl:element> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>



I took the XSLT example from here: http://stackoverflow.com/questions/397426/xsd-class-generation-nested-tables-problem[^]


这篇关于xml到xsd的转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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