views:

379

answers:

3

When I use an XML example file to generate an XSD, using both Visual Studio and Oxygen, it generates a file using tons of <xs:element ref="ELEMENTNAME" />, where elementname is an actual element name. Later in the file, it has an element <xs:element name="ELEMENTNAME" type="xs:string" /> where it defines what that element is. For example, here's an excerpt:

<xs:element name="Header">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="VersionNumber" /> 
      <xs:element ref="BillerGroupID" /> 
      <xs:element ref="BillerGroupShortName" /> 
      <xs:element ref="BillerID" /> 
      <xs:element ref="BillerShortName" /> 
      <xs:element ref="FileIndicator" /> 
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name="VersionNumber" type="xs:string" /> 
<xs:element name="BillerGroupID" type="xs:string" /> 
<xs:element name="BillerGroupShortName" type="xs:string" /> 
<xs:element name="BillerID" type="xs:string" /> 
<xs:element name="BillerShortName" type="xs:string" /> 
<xs:element name="FileIndicator" type="xs:string" />

Here's the problem - I'm using Microsoft Biztalk, and it sees every single "Element" tag as an available schema because they're all at the root level - Header, along with every single child element. I just want to make one schema available - Header, in this case - and hide the rest.

The obvious solution seems to be to manually edit my file to look like this, manually removing the REF statements:

<xs:element name="Header">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="VersionNumber" type="xs:string" /> 
      <xs:element name="BillerGroupID" type="xs:string" /> 
      <xs:element name="BillerGroupShortName" type="xs:string" /> 
      <xs:element name="BillerID" type="xs:string" /> 
      <xs:element name="BillerShortName" type="xs:string" /> 
      <xs:element name="FileIndicator" type="xs:string" /> 
    </xs:sequence>
  </xs:complexType>
</xs:element>

However, since my file is huge and contains thousands of elements, this isn't really feasable. Is there a way to tell my tool to generate the file without using REFs, but instead just placing a copy of the element where it ought to be instead of at the root level?

A: 

I suspect it would be relatively easy to write an XSLT that performs this transformation for you. You can detect the ref attributes and use the value in XPath to find the real definition, then copy that to the output. Shouldn't be very hard to write.

Jeff Yates
I can write the transformation, but why can't the tool just generate the file that way in the first place? I understand the point of the REF, in case you have multiple copies of an element, but it seems like I should be able to check a box that creates a file without them.
rwmnau
Seems you're looking for the tools to workaround the shortcomings of something else. You'll probably find a tool that does it somewhere, but the XSLT is probably quicker to write and means you don't have to add yet another tool.
Jeff Yates
A: 

Which version of VS are you using? I tried your example in Visual Studio 2008, and it generated the XSD the way that you seem to want it:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="Header">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="VersionNumber" type="xs:string" />
        <xs:element name="BillerGroupID" type="xs:string" />
        <xs:element name="BillerGroupShortName" type="xs:string" />
        <xs:element name="BillerID" type="xs:string" />
        <xs:element name="BillerShortName" type="xs:string" />
        <xs:element name="FileIndicator" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

It looks like VS 2005 would do the same thing.

I looked through the VS Options dialogs, but didn't see anything that would control how the XSD is generated. (Maybe there's a configuration file somewhere?)

Dan Breslau
Though this works on the small example I provided, it still generates a schema with plenty of REFs in it when I run the whole XML file through in VS2008. Thanks for the idea, though!
rwmnau
+2  A: 

If you have a Visual Studio BizTalk project you can choose "Add generated items.." and then "Generate schemas".

In the dropdown list choose "Well formed xml", if no one used that option before you must run a script to activate it, see link below.

VS will now generate the kind of schema you want. You will have to adjust data types if VS guessed wrong :)

See this person's sample Add generated schemas

Kind Regards
Martin Bring

Martin Bring
I had to install the "Well-formed XML" option, but it worked like a charm, without a single REF. Thanks for your tip!
rwmnau