我如何获得LinqToXSD正确输出的命名空间preFIX声明?如何获得、声明、正确、空间

2023-09-07 10:04:25 作者:沒心沒肺快樂多

我尝试创建XML数据绑定类与 LinqToXSD 并包含了一些导入的模式XML模式。所有的模式都是 设在这里。

I am experimenting creating XML data binding classes with LinqToXSD and an XML Schema containing a number of imported schemas. All of the schemas are located here.

要做到这一点,我用下面的根模式文档:

To accomplish this, I used the following root schema document:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG" elementFormDefault="unqualified">
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon" schemaLocation="TmatsCommonTypes.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsC" schemaLocation="TmatsCGroup.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsD" schemaLocation="TmatsDGroup.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsG" schemaLocation="TmatsGGroup.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsH" schemaLocation="TmatsHGroup.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsM" schemaLocation="TmatsMGroup.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsP" schemaLocation="TmatsPGroup.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsR" schemaLocation="TmatsRGroup.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsS" schemaLocation="TmatsSGroup.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsT" schemaLocation="TmatsTGroup.xsd"/>
    <xs:import namespace="http://www.spiraltechinc.com/tmats/106-13/TmatsV" schemaLocation="TmatsVGroup.xsd"/>
    <xs:element name="Tmats" type="TmatsG:Tmats">
        <xs:annotation>
            <xs:documentation>Tmats Root</xs:documentation>
        </xs:annotation>
    </xs:element>
</xs:schema>

我创建使用LINQ to XSD类。然后我写了下面的测试:

I created classes using Linq to XSD. I then wrote the following test:

[TestMethod()]
public void TmatsXmlExample4()
{
    Tmats tmats = new Tmats
    {
        ProgramName = "My Program",
        OriginationDate = DateTime.Now,
    };
    tmats.PointOfContact.Add(new PointOfContactType
    {
         Address = "12345 Anywhere Street",
         Agency = "My Agency",
         Name = "Robert Harvey",
         Telephone = "111-222-3333"
    });
    Debug.Print(tmats.ToString());
}

我期望的输出,看上去是这样的:

I expected output that looked something like this:

<Tmats>
  <TmatsG:ProgramName>My Program</TmatsG:ProgramName>
  <TmatsG:OriginationDate>2012-05-09-07:00</TmatsG:OriginationDate>
  <TmatsG:PointOfContact>
    <TmatsCommon:Name>Robert Harvey</TmatsCommon:Name>
   <TmatsCommon:Agency>My Agency</TmatsCommon:Agency>
    <TmatsCommon:Address>12345 Anywhere Street</TmatsCommon:Address>
    <TmatsCommon:Telephone>111-222-3333</TmatsCommon:Telephone>
  </TmatsG:PointOfContact>
</Tmats>

相反,我得到的是这样的:

Instead, what I got was this:

<Tmats>
  <ProgramName xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">My Program</ProgramName>
  <OriginationDate xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">2012-05-09-07:00</OriginationDate>
  <PointOfContact xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">
    <Name xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">Robert Harvey</Name>
    <Agency xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">My Agency</Agency>
    <Address xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">12345 Anywhere Street</Address>
    <Telephone xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">111-222-3333</Telephone>
  </PointOfContact>
</Tmats>

有没有办法让LinqToXSD产生预期的产出?

Is there a way to get LinqToXSD to produce the expected output?

推荐答案

您应该映射每个导入的模式的:

You should map each of the imported schemas:

<?xml version="1.0"?>
    <xs:schema 
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"
        xmlns:TmatsC="http://www.spiraltechinc.com/tmats/106-13/TmatsC"
        xmlns:TmatsD="http://www.spiraltechinc.com/tmats/106-13/TmatsD"
        xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"
        xmlns:TmatsH="http://www.spiraltechinc.com/tmats/106-13/TmatsH"
        xmlns:TmatsM="http://www.spiraltechinc.com/tmats/106-13/TmatsM"
        … 
        elementFormDefault="unqualified">

将elementFormDefault 仅适用于它在架构和它没有替代设置任何包含或进口。

elementFormDefault only applies to the schema that it is in and it doesn't override settings in any include or imports.

如果你想隐藏的命名空间,则所有的模式必须指定将elementFormDefault =不合格的。如果同样要公开的命名空间的每个架构必须指定将elementFormDefault = 的合格的

If you want to hide namespaces then all schemas must specify elementFormDefault="unqualified". Similarly if you want to expose namespaces every schema must specify elementFormDefault="qualified"

更新时间:审查单元测试后:

您的输入:的

<Tmats 
    xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"
    xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">
    <TmatsG:ProgramName>My Project</TmatsG:ProgramName> 
    <TmatsG:OriginationDate>2012-05-15</TmatsG:OriginationDate> 

你的输出:的

<Tmats> 
    <Tmats 
        xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"
        xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">
        <TmatsG:ProgramName>My Project</TmatsG:ProgramName> 
        <TmatsG:OriginationDate>2012-05-15</TmatsG:OriginationDate> 

的突出问题是标签的重复 - 一切看起来确定对我来说,仍然在努力理解为什么会发生

The outstanding problem is the duplication of the tag - everything looks ok to me, still struggling to understand why this is happening.

更新日:

我认为这是在LinqToXSD工具的错误 - 我已经通过每一个组合我能想到的,并不能持续解决您的问题跑,但是...我已成功地解决了&LT; Tmats&GT; 重复问题:

I think there is a bug in the LinqToXSD tool - I've run through every combination I can think of and can't consistently work around your problem, however... I have managed to fix the <Tmats> duplication issue:

在你XmlHelper文件,改变return语句:

In your XmlHelper file, change the return statement:

System.Xml.Linq.XDocument xd = System.Xml.Linq.XDocument.Parse(sb.ToString());
return xd.Root.FirstNode.ToString();

我知道这是一个破解,但它解决了这个问题,你的LoopbackTest通行证。

I know it's a hack, but it fixes the problem and your LoopbackTest passes.

您没有得到任何prefixes如果您创建使用Tmats类的元素,我试过的属性和各种组合最好的,我能做的就是重新连接的命名空间。如果你是与外部系统交换信息的话,我有一个确定

You don't get any prefixes if you create elements using the Tmats class, I've tried various combinations of attributes and the best I could do was re-attach namespaces. If you are exchanging information with an external system then I do have a fix:

使用您的Tmats对象在code, 与命名空间序列化, 在通过XSLT运行它映射ns至prefixes。

我知道这很笨重,但我认为它就是你会得到短期实际固定LinqToXSD code的最好的。

I know it's clunky, but I reckon it's the best you're going to get short of actually fixing the LinqToXSD code.

XSLT映射命名为prefixes(你需要保持设定在样式表申报,并在映射的命名空间:

XSLT to map namespaces to prefixes (you need to maintain the set of namespaces in 'stylesheet' declaration and also in the 'mapper':

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mapper="http://mapper"
    xmlns:TmatsCommon="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"
    xmlns:TmatsC="http://www.spiraltechinc.com/tmats/106-13/TmatsC"
    xmlns:TmatsD="http://www.spiraltechinc.com/tmats/106-13/TmatsD"
    xmlns:TmatsG="http://www.spiraltechinc.com/tmats/106-13/TmatsG"
    xmlns:TmatsH="http://www.spiraltechinc.com/tmats/106-13/TmatsH"
    xmlns:TmatsM="http://www.spiraltechinc.com/tmats/106-13/TmatsM"
    xmlns:TmatsP="http://www.spiraltechinc.com/tmats/106-13/TmatsP"
    xmlns:TmatsR="http://www.spiraltechinc.com/tmats/106-13/TmatsR"
    xmlns:TmatsS="http://www.spiraltechinc.com/tmats/106-13/TmatsS"
    xmlns:TmatsT="http://www.spiraltechinc.com/tmats/106-13/TmatsT"
    xmlns:TmatsV="http://www.spiraltechinc.com/tmats/106-13/TmatsV">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <mapper:namespaces>
    <ns prefix="TmatsCommon" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon"/>
    <ns prefix="TmatsC" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsC"/>
    <ns prefix="TmatsD" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsD"/>
    <ns prefix="TmatsG" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsG"/>
    <ns prefix="TmatsH" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsH"/>
    <ns prefix="TmatsM" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsM"/>
    <ns prefix="TmatsP" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsP"/>
    <ns prefix="TmatsR" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsR"/>
    <ns prefix="TmatsS" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsS"/>
    <ns prefix="TmatsT" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsT"/>
    <ns prefix="TmatsV" uri="http://www.spiraltechinc.com/tmats/106-13/TmatsV"/>
  </mapper:namespaces>
  <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="*[namespace-uri()=document('')/*/mapper:namespaces/*/@uri]">
  <xsl:variable name="vNS" select="document('')/*/mapper:namespaces/*[@uri=namespace-uri(current())]"/>
  <xsl:element name="{$vNS/@prefix}:{local-name()}" namespace="{namespace-uri()}" >
   <xsl:copy-of select="namespace::*[not(. = namespace-uri(current()))]"/>
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

产地:

<Tmats>
  <TmatsG:ProgramName xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">My Program</TmatsG:ProgramName>
  <TmatsG:OriginationDate xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">2012-05-09-07:00</TmatsG:OriginationDate>
  <TmatsG:PointOfContact xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsG">
    <TmatsCommon:Name xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">Robert Harvey</TmatsCommon:Name>
    <TmatsCommon:Agency xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">My Agency</TmatsCommon:Agency>
    <TmatsCommon:Address xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">12345 Anywhere Street</TmatsCommon:Address>
    <TmatsCommon:Telephone xmlns="http://www.spiraltechinc.com/tmats/106-13/TmatsCommon">111-222-3333</TmatsCommon:Telephone>
  </TmatsG:PointOfContact>
</Tmats>

好了,这是很不理想,但你的code正常工作,内部的项目时,它只有当你需要与其他人,你就需要修正XML输出​​交互(记得更改将elementFormDefault =合格(或删除它)在XSD) - 如果缓存的XSLT作为 XslCompiledTransform 你几乎没有注意到它发生在所有

Ok, so this is far from ideal, but your code works fine internally to the project it only when you need to interact with other people that you'd need to fix the xml output (remember to change elementFormDefault="qualified" (or remove it) in your XSD) - if you cache the XSLT as a XslCompiledTransform you'd barely notice it happening at all.