I'm brand new to SQL and SQL Server Management Studio, so bear with me. Here's a shortened version of my query:
SELECT *
FROM [dbo].[BranchLocations] WHERE NOT (PropertyOwnerType IS NULL)
FOR XML PATH('LocationData'), ROOT('MainRoot')
This gives me exactly what I need except for the schema. I don't want to automatically generate a schema because the service to which I upload the XML data only accepts a specific schema structure, and I would prefer to control it.
My schema looks similar to this:
<xs:schema id="LocationData_ds" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="LocationData_ds" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="LocationData">
<xs:complexType>
<xs:sequence>
<xs:element name="EntityID" type="xs:string" />
<xs:element name="AddressLine" type="xs:string" minOccurs="0" />
<xs:element name="Locality" type="xs:string" minOccurs="0" />
<xs:element name="AdminDistrict" type="xs:string" minOccurs="0" />
<xs:element name="PostalCode" type="xs:string" minOccurs="0" />
<xs:element name="CountryRegion" type="xs:string" minOccurs="0" />
<xs:element name="Latitude" type="xs:double" minOccurs="0" />
<xs:element name="Longitude" type="xs:double" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//LocationData" />
<xs:field xpath="EntityID" />
</xs:unique>
</xs:element>
</xs:schema>
My query returns this:
<MainRoot>
<LocationData>
<EntityID>10010000</EntityID>
<AddressLine>1234 address<\/AddressLine>
<Locality>Converse</Locality>
<AdminDistrict>TX</AdminDistrict>
<PostalCode>12345</PostalCode>
<CountryRegion>US</CountryRegion>
</LocationData>
<LocationData>
<EntityID>70390000</EntityID>
<AddressLine>1234 address<\/AddressLine>
<Locality>Denver</Locality>
<AdminDistrict>CO</AdminDistrict>
<PostalCode>12345</PostalCode>
<CountryRegion>US</CountryRegion>
</LocationData>
...
<MainRoot>
And what I need is this:
(and I need ti include<?xml version="1.0" encoding="utf-8" standalone="yes"?> at the top as well)
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<MainRoot>
<xs:schema id="LocationData_ds" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="LocationData_ds" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="LocationData">
<xs:complexType>
<xs:sequence>
<xs:element name="EntityID" type="xs:string" />
<xs:element name="AddressLine" type="xs:string" minOccurs="0" />
<xs:element name="Locality" type="xs:string" minOccurs="0" />
<xs:element name="AdminDistrict" type="xs:string" minOccurs="0" />
<xs:element name="PostalCode" type="xs:string" minOccurs="0" />
<xs:element name="CountryRegion" type="xs:string" minOccurs="0" />
<xs:element name="Latitude" type="xs:double" minOccurs="0" />
<xs:element name="Longitude" type="xs:double" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//LocationData" />
<xs:field xpath="EntityID" />
</xs:unique>
</xs:element>
</xs:schema>
<LocationData>
<EntityID>10010000</EntityID>
<AddressLine>1234 address<\/AddressLine>
<Locality>Converse</Locality>
<AdminDistrict>TX</AdminDistrict>
<PostalCode>12345</PostalCode>
<CountryRegion>US</CountryRegion>
</LocationData>
<LocationData>
<EntityID>70390000</EntityID>
<AddressLine>1234 address<\/AddressLine>
<Locality>Denver</Locality>
<AdminDistrict>CO</AdminDistrict>
<PostalCode>12345</PostalCode>
<CountryRegion>US</CountryRegion>
</LocationData>
...
<MainRoot>
Looking through the documentation has led me here:
https://learn.microsoft.com/en-us/sql/t-sql/xml/insert-xml-dml?view=sql-server-ver15
It isn't working, but I think I might be on the right track.
These answers only relate to generating a schema:
convert database table into XML schema filehow to create XML schema from an existing database in SQL Server 2008
declare #BranchLocations table
(
EntityId varchar(20),
AddressLine varchar(20),
Locality varchar(20),
AdminDistrict varchar(20),
PostalCode varchar(20),
CountryRegion varchar(20),
PropertyOwnerType varchar(20)
);
insert into #BranchLocations(EntityId, AddressLine, Locality, AdminDistrict, PostalCode, CountryRegion, PropertyOwnerType)
values
('10010000', '1234 address', 'Converse', 'TX', '12345', 'US', 'x'),
('70390000', '1234 address', 'Denver', 'CO', '12345', 'US', 'x');
declare #myschema xml = N'<xs:schema id="LocationData_ds" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="LocationData_ds" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="LocationData">
<xs:complexType>
<xs:sequence>
<xs:element name="EntityID" type="xs:string" />
<xs:element name="AddressLine" type="xs:string" minOccurs="0" />
<xs:element name="Locality" type="xs:string" minOccurs="0" />
<xs:element name="AdminDistrict" type="xs:string" minOccurs="0" />
<xs:element name="PostalCode" type="xs:string" minOccurs="0" />
<xs:element name="CountryRegion" type="xs:string" minOccurs="0" />
<xs:element name="Latitude" type="xs:double" minOccurs="0" />
<xs:element name="Longitude" type="xs:double" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//LocationData" />
<xs:field xpath="EntityID" />
</xs:unique>
</xs:element>
</xs:schema>';
declare #res nvarchar(max);
select #res = N'<?xml version="1.0" encoding="utf-8" standalone="yes"?>' +
(
select *
from
(
select #myschema as '*'
union all
select
(
select *
FROM #BranchLocations
WHERE PropertyOwnerType is not null
FOR XML PATH('LocationData')
)
) as src
for xml path(''), root('Mainroot')
);
--test, show result
select 1 as tag, 0 as parent,
#res+'</foo>' as 'test!1!!xmltext'
for xml explicit;
--or
select #res = null;
declare #myxml xml = ( select *
FROM #BranchLocations
WHERE PropertyOwnerType is not null
and 1=2
FOR XML PATH('LocationData'));
--null result when empty resultset from table
select #res =
N'<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
+(select #myschema,#myxml for xml path(''), root('Mainroot'))
where #myxml is not null;
select #res;
What you are looking for is called an Inline XSD Schema.
Here is BOL link: Generate an Inline XSD Schema
Unfortunately, it is very limited: "...You can specify XMLSCHEMA only in RAW and AUTO mode, not in EXPLICIT mode...". Same with the PATH mode.
Additional limitation is that SQL Server keeps XML as UTF-16, and strips an XML prolog from any XML data type internally.
To achieve what you need everything needs to be done manually, i.e. stitch together a prolog, XML Schema, and actual XML.
Related
I have a very large xml field inside of a SQL Database that I have to edit regularly. I wrote the following Query to change that code which was easy following the xml path to get to what needed changed.
However I found more things further down in SQL that needs altered and I can not figure out the xml path to edit to make that change.
I've been utilizing the following Query to change just the username
DECLARE #config_xml2 xml
select #config_xml2 = configuration_xml from tblDatafeed where datafeed_name = 'DatafeedName' update tbldatafeed
set configuration_xml.modify('replace value of (//*:NetworkCredentialWrapper/#UserName)[1] with sql:variable("#LOGIN")') where datafeed_name ='DataFeedName'
Which is used to change the Username in the following:
<Tokens>
<Token name="DataFileDirectoryName" />
<Token name="DataFileName" />
<Token name="DataFileExtension" />
<Token name="LastRunTime" />
<Token name="LastFileProcessed" />
</Tokens>
<Transporter>
<transporters:WebServiceTransportActivity xmlns:transporters="clr-namespace:ArcherTech.DataFeed.Activities.Transporters;assembly=ArcherTech.DataFeed" xmlns:out="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:compModel="clr-namespace:ArcherTech.DataFeed.ComponentModel;assembly=ArcherTech.DataFeed" xmlns:channel="clr-namespace:ArcherTech.DataFeed.Engine.Channel;assembly=ArcherTech.DataFeed" xmlns:engine="clr-namespace:ArcherTech.DataFeed.Engine;assembly=ArcherTech.DataFeed" xmlns:kernel="clr-namespace:ArcherTech.Kernel.Channel;assembly=ArcherTech.Kernel" xmlns="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:schema="clr-namespace:System.Xml.Schema;assembly=System.Xml" xmlns:xmlLinq="clr-namespace:System.Xml.Linq;assembly=System.Xml" xmlns:domain="clr-namespace:ArcherTech.Common.Domain;assembly=ArcherTech.Common" xmlns:s="clr-namespace:System;assembly=mscorlib" x:Key="transportActivity" SearchType="ReportId" Uri="Type URL Here" RecordsPerFile="100" ReportID="" UseWindowsAuth="false" IsWindowsAuthSpecific="false" WindowsAuthUserName="" WindowsAuthPassword="" WindowsAuthDomain="" ProxyName="" ProxyPort="" ProxyUsername="" ProxyPassword="" ProxyDomain="" IsProxyActive="False" ProxyOption="None" InstanceName="Type Instance Name Here" TempFileOnSuccessAction="DoNothing" TempFileOnSuccessRenameString="" TempFileOnErrorAction="DoNothing" TempFileOnErrorRenameString="" Transform="{engine:DataFeedBinding Path=Transform}" SessionContext="{engine:DataFeedBinding Path=Session}">
<transporters:ArcherWebServiceTransportActivity.Credentials>
<NetworkCredentialWrapper UserName="" />
</transporters:ArcherWebServiceTransportActivity.Credentials>
</transporters:ArcherWebServiceTransportActivity>
</Transporter>
<Iterator>
<XmlIterator>
But now I need to alter a string that a lot more complex and I am not sure my xml location is right. I have tried using //*NewSourceName1/#Calculation but it doesnt seem work:
Line of code Here I am trying to edit:
<xs:element name="NewSourceName1" type="xs:string" nillable="true" c:Name="Dashboard_Instance_Name" c:Calculation="=STATIC("MYSTRINGOFTEXTHERE")" dfm:SimpleFieldType="StaticText" />
Full xml it falls under
<Action>
<DataTransfer TargetType="Application" TargetLevel="20595ffc-afd5-4d34-babb-c8fa67c610ee" DisableValidation="true" PreValidate="false" InsertRecords="true" UpdateRecords="false" SyncAction="None" SyncFlagField="00000000-0000-0000-0000-000000000000" SyncFlagValue="00000000-0000-0000-0000-000000000000">
<Source>
<Schema><Schemas>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.archer-tech.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dfm="http://schemas.archer-tech.com/2009/xaml/datafeed" xmlns:c="http://archertech.com/ChannelFramework/Extensions" dfm:ComplexFieldType="None">
<xs:element name="ArcherRecords" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="ArcherRecord" nillable="true" c:Name="ArcherRecord" dfm:ComplexFieldType="None" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" nillable="true" c:Name="Name" />
<xs:element name="Container_Type" nillable="true" c:Name="Container_Type">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" type="xs:string" nillable="true" c:Name="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Path" type="xs:string" nillable="true" c:Name="Path" />
<xs:element name="Abbreviation_Unique_Key" type="xs:string" nillable="true" c:Name="Abbreviation_Unique_Key" />
<xs:element name="Container_Owner" nillable="true" c:Name="Container_Owner">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" type="xs:string" nillable="true" c:Name="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Send_Upstream" nillable="true" c:Name="Send_Upstream">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" type="xs:string" nillable="true" c:Name="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewSourceName" type="xs:string" nillable="true" c:Name="Iteration" c:Calculation="=IF(AND(HOUR(NOW())>=0,HOUR(NOW())<8), "ONE",
 IF(AND(HOUR(NOW())>=8,HOUR(NOW())<16), "TWO", "THREE"))" dfm:SimpleFieldType="CalculatedField" />
<xs:element name="NewSourceName1" type="xs:string" nillable="true" c:Name="Dashboard_Instance_Name" c:Calculation="=STATIC("MyTextHere")" dfm:SimpleFieldType="StaticText" />
<xs:element name="NewSourceName2" type="xs:string" nillable="true" c:Name="Date_Created" c:Calculation="=DATEFORMAT(TODAY(), "MM/dd/yyyy")" dfm:SimpleFieldType="CalculatedField" />
<xs:element name="NewSourceName3" type="xs:string" nillable="true" c:Name="Created_By" c:Calculation="=STATIC("Local Dashboard")" dfm:SimpleFieldType="StaticText" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</Schemas></Schema>
</Source>
Hopeful out come (Look for MYSTRINGOFTEXTHERE or if I could just edit all of =STATIC("MYSTRINGOFTEXTHERE")):
<xs:element name="NewSourceName1" type="xs:string" nillable="true" c:Name="Dashboard_Instance_Name" c:Calculation="=STATIC("MYSTRINGOFTEXTHERE")" dfm:SimpleFieldType="StaticText" />
What I tried that didn't work
DECLARE #config_xml2 xml
select #config_xml2 = configuration_xml from tblDatafeed where datafeed_name = 'DatafeedName' update tbldatafeed
set configuration_xml.modify('replace value of (//*:NewSourceName1/#Calculation)[1] with sql:variable("#CoolVariableName")') where datafeed_name ='DataFeedName'
Any help would be greatly appreciated.
The XPath expression was completely off, so I adjusted it.
In XML, there are only five built-in character entities: <, >, &, " and ' for <, >, &, " and ' respectively. I replaced them with their literals. Please try the following.
SQL
DECLARE #tbl TABLE (id int IDENTITY PRIMARY KEY, configuration_xml XML);
INSERT INTO #tbl (configuration_xml)
VALUES
(N'<Action>
<DataTransfer TargetType="Application" TargetLevel="20595ffc-afd5-4d34-babb-c8fa67c610ee" DisableValidation="true" PreValidate="false" InsertRecords="true" UpdateRecords="false" SyncAction="None" SyncFlagField="00000000-0000-0000-0000-000000000000" SyncFlagValue="00000000-0000-0000-0000-000000000000">
<Source>
<Schema>
<Schemas>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.archer-tech.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dfm="http://schemas.archer-tech.com/2009/xaml/datafeed" xmlns:c="http://archertech.com/ChannelFramework/Extensions" dfm:ComplexFieldType="None">
<xs:element name="ArcherRecords" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="ArcherRecord" nillable="true" c:Name="ArcherRecord" dfm:ComplexFieldType="None" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" nillable="true" c:Name="Name" />
<xs:element name="Container_Type" nillable="true" c:Name="Container_Type">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" type="xs:string" nillable="true" c:Name="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Path" type="xs:string" nillable="true" c:Name="Path" />
<xs:element name="Abbreviation_Unique_Key" type="xs:string" nillable="true" c:Name="Abbreviation_Unique_Key" />
<xs:element name="Container_Owner" nillable="true" c:Name="Container_Owner">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" type="xs:string" nillable="true" c:Name="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Send_Upstream" nillable="true" c:Name="Send_Upstream">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" type="xs:string" nillable="true" c:Name="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewSourceName" type="xs:string" nillable="true" c:Name="Iteration" c:Calculation="=IF(AND(HOUR(NOW())>=0,HOUR(NOW())<8), "ONE",
 IF(AND(HOUR(NOW())>=8,HOUR(NOW())<16), "TWO", "THREE"))" dfm:SimpleFieldType="CalculatedField" />
<xs:element name="NewSourceName1" type="xs:string" nillable="true" c:Name="Dashboard_Instance_Name" c:Calculation="=STATIC("MyTextHere")" dfm:SimpleFieldType="StaticText" />
<xs:element name="NewSourceName2" type="xs:string" nillable="true" c:Name="Date_Created" c:Calculation="=DATEFORMAT(TODAY(), "MM/dd/yyyy")" dfm:SimpleFieldType="CalculatedField" />
<xs:element name="NewSourceName3" type="xs:string" nillable="true" c:Name="Created_By" c:Calculation="=STATIC("Local Dashboard")" dfm:SimpleFieldType="StaticText" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</Schemas>
</Schema>
</Source>
</DataTransfer>
</Action>');
DECLARE #CoolVariableName varchar(30) = 'Whatever Value';
UPDATE #tbl
SET configuration_xml.modify('replace value of (//xs:element[#name="NewSourceName1"]/#*:Calculation)[1] with sql:variable("#CoolVariableName")');
SELECT * FROM #tbl;
<?xml version="1.0"?>
<DataTable>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="point24" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="point24">
<xs:complexType>
<xs:sequence>
<xs:element name="RoadCode" type="xs:string" minOccurs="0" />
<xs:element name="Chainage" type="xs:string" minOccurs="0" />
<xs:element name="Dir" type="xs:string" minOccurs="0" />
<xs:element name="Latitude" type="xs:string" minOccurs="0" />
<xs:element name="Longitude" type="xs:string" minOccurs="0" />
<xs:element name="test1" type="xs:string" minOccurs="0" />
<xs:element name="CompositeKey" type="xs:string" minOccurs="0" />
<xs:element name="AddedBy" type="xs:string" minOccurs="0" />
<xs:element name="AddedOn" type="xs:dateTime" minOccurs="0" />
<xs:element name="ModifiedBy" type="xs:string" minOccurs="0" />
<xs:element name="ModifiedOn" type="xs:dateTime" minOccurs="0" />
<xs:element name="MasterColumnKey" type="xs:string" minOccurs="0" />
<xs:element name="LinkCode" type="xs:string" minOccurs="0" />
<xs:element name="SurveyId" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<DocumentElement>
<point24 diffgr:id="point241" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<RoadCode>SH0008</RoadCode>
<Chainage>5894</Chainage>
<Dir>I</Dir>
<Latitude>6.3</Latitude>
<Longitude>2.5</Longitude>
<test1>56</test1>
<CompositeKey>SH00085894IJAN2016</CompositeKey>
<AddedBy>dev</AddedBy>
<AddedOn>2017-11-29T10:16:09.3488278+05:30</AddedOn>
<MasterColumnKey>SH0008589456JAN2016</MasterColumnKey>
<LinkCode>LI:SH00084502</LinkCode>
<SurveyId>JAN2016</SurveyId>
</point24>
</DocumentElement>
</diffgr:diffgram>
</DataTable>
Am publishing a datatable into queue using rabbit mq and trying to consume the datable from console application.
Unable to get the data.
I am using rabbit mq 3.6.14 version with erlang 20.1Rabbit MQ management UI
Am adding the datatable to queue from web application like this..
Producer web application code
Am retreiving the message from queue from console application
Consumer application code
here is my queue binding
enter image description here
Queue Details
Binding Details
please suggest.
I have an XML file with the following content (some sensitive data replaced with 'DummyData')
<Alert>
<AlertID>23210B</AlertID>
<Title>DummyData</Title>
<PublishDate>3/31/2014 12:00:00 AM</PublishDate>
<Severity>03</Severity>
<ResponseNecessary>Yes</ResponseNecessary>
<ThreatDate>6/2/2014 12:00:00 AM</ThreatDate>
<DueDate>
</DueDate>
<SystemStandard>Yes</SystemStandard>
<Type>DummyData</Type>
<Overview>DummyDataDummyDataDummyDataDummyDataDummyDataDummyDataDummyDataDummyDataDummyDataDummyData</Overview>
<NIRTAnalysis><p>N/A</p></NIRTAnalysis>
<ThreatAsessment><p>N/A</p></ThreatAsessment>
<ProductsAffected><p>DummyData 10 SP4 LTSS</p></ProductsAffected>
<RequiredActions>DummyDataDummyDataDummyDataDummyDataDummyData</RequiredActions>
<AdditionalInformation>DummyDataDummyDataDummyDataDummyDataDummyData</AdditionalInformation>
<ExternalReference>DummyDataDummyDataDummyDataDummyDataDummyDataDummyData</ExternalReference>
<PatchID>
</PatchID>
</Alert>
And the following xsd file generated by SSIS:
<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Alert">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="AlertID" type="xs:string" />
<xs:element minOccurs="0" name="Title" type="xs:string" />
<xs:element minOccurs="0" name="PublishDate" type="xs:string" />
<xs:element minOccurs="0" name="Severity" type="xs:unsignedByte" />
<xs:element minOccurs="0" name="ResponseNecessary" type="xs:string" />
<xs:element minOccurs="0" name="ThreatDate" type="xs:string" />
<xs:element minOccurs="0" name="DueDate" type="xs:string" />
<xs:element minOccurs="0" name="SystemStandard" type="xs:string" />
<xs:element minOccurs="0" name="Type" type="xs:string" />
<xs:element minOccurs="0" name="Overview" type="xs:string" />
<xs:element minOccurs="0" name="NIRTAnalysis" type="xs:string" />
<xs:element minOccurs="0" name="ThreatAsessment" type="xs:string" />
<xs:element minOccurs="0" name="ProductsAffected" type="xs:string" />
<xs:element minOccurs="0" name="RequiredActions" type="xs:string" />
<xs:element minOccurs="0" name="AdditionalInformation" type="xs:string" />
<xs:element minOccurs="0" name="ExternalReference" type="xs:string" />
<xs:element minOccurs="0" name="PatchID" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
My problem is when I use XML Source to load the file but the source doesn't recognize the columns. I'm guessing it thinks the file has nested nodes because the only way I can get it to see any columns is with the following XSD below, which will see all the columns but all as separate output:
<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Alert">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="AlertID" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="Title" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="PublishDate" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="Severity" type="xs:unsignedByte" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="ResponseNecessary" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="ThreatDate" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="DueDate" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="SystemStandard" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="Type" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="Overview" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="NIRTAnalysis" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="ThreatAsessment" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="ProductsAffected" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="RequiredActions" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="AdditionalInformation" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="ExternalReference" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="PatchID" type="xs:string" />
</xs:sequence>
</xs:complexType>
I don't know a lot about XLST, but I know I can flatten the file that way, but to me, it is flat. Are there any suggestions as to what I'm doing wrong? I believe I just have an xsd problem, but what you see was generated by SSIS.
It seems that the problem is related to the XML itself.
Please add a root element to the xml file and try again with a new SSIS-generated xsd after adding the root.
xml should look like:
<?xml version="1.0"?>
<Root>
<Alert>
....
</Alert>
</Root>
I am getting info from a database and adding it to a dataset. Then I create a XML with this info. I would want to have the XML, but also the datatypes in the header. Is it possible?
Here my code to write the XML:
//Fill dataset with different datatable:
//first datatable
DataSet imports = new DataSet("import");
NpgsqlDataAdapter daImport = new NpgsqlDataAdapter("select field1, field2 field3 from table1", _connPg);
daImport.FillSchema(imports, SchemaType.Source, "table1");
daImport.Fill(imports, "table1");
//seconda datatable
NpgsqlDataAdapter daImport1 = new NpgsqlDataAdapter("select field1, field2 field3 from table2", _connPg);
daImport1.FillSchema(imports, SchemaType.Source, "table2");
daImport1.Fill(imports, "table2");
//insert relation
DataRelation relation = new DataRelation("rel_rel1", imports.Tables["table1"].Columns["field1"], imports.Tables["table2"].Columns["field2"], true);
relation.Nested = true;
imports.Relations.Add(relation);
//write xml
imports.WriteXml("dataImport.xml");
And this is my xml
<?xml version="1.0" standalone="yes"?>
<import>
<table1>
<field1>1
</field1>
<field2>name
</field2>
<field3>surname
</field3>
<table2>
<field1>somedata
</field1>
<field2>1
</field2>
<field3>otherdata
</field3>
</table2>
</table1>
</import>
What I want?
I would want to get this XML but with the types of the fields defined in the header.
Thanks!
If you want the schema to be serialized with the data, just change :
imports.WriteXml("dataImport.xml");
to :
imports.WriteXml("dataImport.xml", XmlWriteMode.WriteSchema);
It will produce the following XML :
<?xml version="1.0" standalone="yes"?>
<import>
<xs:schema id="import" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="import" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="table1">
<xs:complexType>
<xs:sequence>
<xs:element name="field1" type="xs:int" />
<xs:element name="field2" type="xs:string" minOccurs="0" />
<xs:element name="field3" type="xs:boolean" minOccurs="0" />
<xs:element name="table2" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="field1" type="xs:int" />
<xs:element name="field2" type="xs:int" minOccurs="0" />
<xs:element name="field3" type="xs:double" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="table2_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//table2" />
<xs:field xpath="field1" />
</xs:unique>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//table1" />
<xs:field xpath="field1" />
</xs:unique>
<xs:keyref name="FK_T1T2" refer="Constraint1" msdata:IsNested="true">
<xs:selector xpath=".//table2" />
<xs:field xpath="field2" />
</xs:keyref>
</xs:element>
</xs:schema>
<table1>
<field1>0</field1>
<field2>test</field2>
<field3>false</field3>
<table2>
<field1>0</field1>
<field2>0</field2>
<field3>0</field3>
</table2>
<table2>
<field1>1</field1>
<field2>0</field2>
<field3>1.25</field3>
</table2>
</table1>
<table1>
<field1>1</field1>
<field2>test 2</field2>
<field3>true</field3>
</table1>
<table1>
<field1>2</field1>
<field2>test 3</field2>
<field3>false</field3>
<table2>
<field1>2</field1>
<field2>2</field2>
<field3>2.66</field3>
</table2>
</table1>
</import>
I want to read an XML file and split the data into a parent and 2 child tables. Here is what the SQL schema where the data will go and the XSD. I'm thinking SSIS is what I want to use but not sure how to go about it. Should i iterate through the children and then insert the rows using code. Any ideas would be helpful. I am willing to use something other than SSIS. I using MS SQL Server 2008.
<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MonthlyReportForm">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element minOccurs="0" name="ReportingDate" type="xs:string" />
<xs:element minOccurs="0" name="ReportingYear" type="xs:unsignedShort" />
<xs:element minOccurs="0" name="ReportingRegion" type="xs:string" />
<xs:element minOccurs="0" name="TeamMembersName" type="xs:string" />
<xs:element minOccurs="0" name="chkTraining" type="xs:unsignedByte" />
<xs:element minOccurs="0" name="chkVernacularMaterials" type="xs:unsignedByte" />
<xs:element minOccurs="0" name="RDHelpText" type="xs:string" />
<xs:element minOccurs="0" name="chkOutOfCountryForm" type="xs:unsignedByte" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="TravelData">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="TravelRowLocation" type="xs:string" />
<xs:element minOccurs="0" name="TravelRowTeam" type="xs:string" />
<xs:element minOccurs="0" name="TravelRowThisMonth" type="xs:unsignedByte" />
<xs:element minOccurs="0" name="TravelRowNextMonth" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" name="TrainingReport">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="TrainingData">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="TrainerName" type="xs:string" />
<xs:element minOccurs="0" name="TraineeGender" type="xs:string" />
<xs:element minOccurs="0" name="TraineeAge" type="xs:decimal" />
<xs:element minOccurs="0" name="TraineeLanguage" type="xs:string" />
<xs:element minOccurs="0" name="TraineeEducation" type="xs:string" />
<xs:element minOccurs="0" name="TraineeProvince" type="xs:string" />
<xs:element minOccurs="0" name="TrainingPrepHrs" type="xs:decimal" />
<xs:element minOccurs="0" name="TrainingTrainHrs" type="xs:decimal" />
<xs:element minOccurs="0" name="TrainingArea" type="xs:unsignedByte" />
<xs:element minOccurs="0" name="TrainingActivity" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>