Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
NicJ
Starting Member
15 Posts |
Posted - 2009-05-28 : 09:50:52
|
| OKCreate a database called dummythen run this to generate some tables:-=======================================USE [dummy]GOIF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Linxdatatypes]') AND type in (N'U'))DROP TABLE [dbo].[Linxdatatypes]GOUSE [dummy]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[Linxdatatypes]( [types] [varchar](20) NOT NULL, [typesid] [smallint] NOT NULL, [Name] [varchar](40) NOT NULL, [Description] [varchar](60) NOT NULL, [Basictype] [varchar](40) NULL, [FK_Alias] [smallint] NULL, [DecimalPlaces] [smallint] NULL,PRIMARY KEY CLUSTERED ( [typesid] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOIF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[fk_typesid]') AND parent_object_id = OBJECT_ID(N'[dbo].[Linxdatatypesvalues]'))ALTER TABLE [dbo].[Linxdatatypesvalues] DROP CONSTRAINT [fk_typesid]GOIF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Linxdatatypesvalues]') AND type in (N'U'))DROP TABLE [dbo].[Linxdatatypesvalues]GOCREATE TABLE [dbo].[Linxdatatypesvalues]( [typesid] [smallint] NOT NULL, [value] [smallint] NOT NULL, [Description] [varchar](60) NOT NULL,PRIMARY KEY CLUSTERED ( [typesid] ASC, [value] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGOALTER TABLE [dbo].[Linxdatatypesvalues] WITH CHECK ADD CONSTRAINT [fk_typesid] FOREIGN KEY([typesid])REFERENCES [dbo].[Linxdatatypes] ([typesid])GOALTER TABLE [dbo].[Linxdatatypesvalues] CHECK CONSTRAINT [fk_typesid]GO=============================Run this to add some data==================USE [dummy];SET NOCOUNT ON;SET XACT_ABORT ON;GOBEGIN TRANSACTION;INSERT INTO [dbo].[Linxdatatypes]([types], [typesid], [Name], [Description], [Basictype], [FK_Alias], [DecimalPlaces])SELECT N'NumericType', 0, N'U32_t', N'Unsigned 32-bit value', N'unsigned long', NULL, NULL UNION ALLSELECT N'NumericType', 1, N'U16_t', N'Unsigned 16-bit value', N'unsigned short', NULL, NULL UNION ALLSELECT N'NumericType', 2, N'U8_t', N'Unsigned 8-bit value', N'unsigned char', NULL, NULL UNION ALLSELECT N'NumericType', 3, N's32_t', N'Signed 32-bit value', N'unsigned long', NULL, NULL UNION ALLSELECT N'NumericType', 4, N's16_t', N'Signed 16-bit value', N'unsigned short', NULL, NULL UNION ALLSELECT N'NumericType', 5, N's8_t', N'Signed 8-bit value', N'unsigned char', NULL, NULL UNION ALLSELECT N'NumericType', 6, N'boolean_t', N'Boolean value', N'unsigned long', NULL, NULL UNION ALLSELECT N'NumericType', 7, N'extendedData_t', N'Extended data pointer', N'unsigned char*', NULL, NULL UNION ALLSELECT N'NumericType', 8, N'voltage_t', N'Volts in tenths of a Volt', NULL, 0, 1 UNION ALLSELECT N'NumericType', 9, N'test_t', N'Test data type', NULL, 0, NULL UNION ALLSELECT N'EnumType', 10, N'enumLightbarFlashingMode_t', N'Lightbar settings|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 11, N'enumButtonStatus_t', N'Push button status|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 12, N'enumButtonPress_t', N'Push button pushes|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 13, N'enumIgnitionSwitch_t', N'State of ignition switch|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 14, N'enumVehicleBatteryState_t', N'Vehicle battery state|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 15, N'enumVehicleBatteryThreshold_t', N'Threshold voltages for vehicle battery|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 16, N'enumLightbarAdvancedFlashingMode_t', N'Lightbar settings|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 17, N'enumDeviceState_t', N'State of device|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 18, N'enumCameraFlashState_t', N'Camera flash state|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 19, N'enumDeviceRequiredState_t', N'Required state of device|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 20, N'enumDockLinxPresenceState_t', N'LINX present in dock state|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 21, N'enumDockLockStatus_t', N'Status of docking station lock|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 22, N'enumDockStatus_t', N'Status of docking|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 23, N'enumDockPowerStatus_t', N'Power state in docking station|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 24, N'enumLedColour_t', N'Colour of LED|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 25, N'enumLedState_t', N'On, off or flashing state of LED|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 26, N'enumLedPairState_t', N'On, off or flashing state of LED|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 27, N'enumTemperatureState_t', N'LINX thermal state|', NULL, NULL, NULL UNION ALLSELECT N'EnumType', 28, N'enumTemperatureThreshold_t', N'Threshold temperatures for LINX|', NULL, NULL, NULL UNION ALLSELECT N'NumericType', 29, N'temperature_t', N'Temperature in tenths of a degree Celsius', NULL, 3, 1 UNION ALLSELECT N'NumericType', 30, N'batteryLevel_t', N'Battery charge level percentage', NULL, 0, NULL UNION ALLSELECT N'EnumType', 31, N'enumBatteryState_t', N'Battery state|', NULL, NULL, NULLCOMMIT;RAISERROR (N'[dbo].[Linxdatatypes]: Loaded the datatypes', 10, 1) WITH NOWAIT;GOBEGIN TRANSACTION;INSERT INTO [dbo].[Linxdatatypesvalues]([typesid], [value], [Description])SELECT 10, 0, N'FlashFast' UNION ALLSELECT 10, 1, N'FlashSlow' UNION ALLSELECT 11, 1, N'ButtonNotPushed' UNION ALLSELECT 11, 2, N'ButtonPushed' UNION ALLSELECT 12, 1, N'ButtonMomentaryPush' UNION ALLSELECT 12, 2, N'ButtonPushAndHold' UNION ALLSELECT 12, 3, N'ButtonRelease' UNION ALLSELECT 13, 1, N'IgnitionSwitchOff' UNION ALLSELECT 13, 2, N'IgnitionSwitchOn' UNION ALLSELECT 14, 1, N'VehicleBatteryStateNormal' UNION ALLSELECT 14, 2, N'VehicleBatteryStateLow' UNION ALLSELECT 14, 3, N'VehicleBatteryStateVeryLow' UNION ALLSELECT 14, 4, N'VehicleBatteryStateHigh' UNION ALLSELECT 15, 1, N'VehicleBatteryLowThresholdDown' UNION ALLSELECT 15, 2, N'VehicleBatteryLowThresholdUp' UNION ALLSELECT 15, 3, N'VehicleBatteryVeryLowThresholdDown' UNION ALLSELECT 15, 4, N'VehicleBatteryVeryLowThresholdUp' UNION ALLSELECT 15, 5, N'VehicleBatteryHighThresholdDown' UNION ALLSELECT 15, 6, N'VehicleBatteryHighThresholdUp' UNION ALLSELECT 16, 0, N'Flash1Hz' UNION ALLSELECT 16, 1, N'Flash2Hz' UNION ALLSELECT 16, 2, N'Flash4Hz' UNION ALLSELECT 16, 3, N'Flash8Hz' UNION ALLSELECT 16, 4, N'Flash16Hz' UNION ALLSELECT 17, 1, N'DevicePowerOff' UNION ALLSELECT 17, 2, N'DeviceSwitchingOn' UNION ALLSELECT 17, 3, N'DevicePowerOn' UNION ALLSELECT 17, 4, N'DeviceSwitchingOff' UNION ALLSELECT 17, 6, N'DeviceProhibited' UNION ALLSELECT 18, 1, N'CameraFlashOff' UNION ALLSELECT 18, 2, N'CameraFlashTorch' UNION ALLSELECT 18, 3, N'CameraFlashPhoto' UNION ALLSELECT 19, 1, N'DeviceSetPowerOff' UNION ALLSELECT 19, 3, N'DeviceSetPowerOn' UNION ALLSELECT 19, 5, N'DeviceSetDefault' UNION ALLSELECT 19, 6, N'DeviceSetProhibited' UNION ALLSELECT 20, 1, N'DockLinxAbsent' UNION ALLSELECT 20, 2, N'DockLinxPresent' UNION ALLSELECT 21, 1, N'DockLockLocked' UNION ALLSELECT 21, 2, N'DockLockUnlocking' UNION ALLSELECT 21, 3, N'DockLockUnlocked' UNION ALLSELECT 21, 4, N'DockLockLocking' UNION ALLSELECT 22, 1, N'DockUndockRequestPending' UNION ALLSELECT 22, 2, N'DockUndocking' UNION ALLSELECT 22, 3, N'DockUndocked' UNION ALLSELECT 22, 4, N'DockDockedVehicle' UNION ALLSELECT 22, 5, N'DockDockedDeskstation' UNION ALLSELECT 22, 6, N'DockDockedMobileOffice' UNION ALLSELECT 23, 1, N'DockPowerOff' UNION ALLSELECT 23, 2, N'DockPowerOn' UNION ALLSELECT 24, 1, N'LedRed' UNION ALLSELECT 24, 2, N'LedYellow' UNION ALLSELECT 24, 3, N'LedGreen' UNION ALLSELECT 24, 4, N'LedBlue' UNION ALLSELECT 25, 1, N'LedOff' UNION ALLSELECT 25, 2, N'LedOn' UNION ALLSELECT 25, 5, N'LedFlash' UNION ALLSELECT 26, 1, N'LedPairBothOff' UNION ALLSELECT 26, 2, N'LedPairColour1' UNION ALLSELECT 26, 3, N'LedPairColour2' UNION ALLSELECT 26, 4, N'LedPairBothOn' UNION ALLSELECT 26, 5, N'LedPairColour1Flash' UNION ALLSELECT 26, 6, N'LedPairColour2Flash' UNION ALLSELECT 26, 7, N'LedPairBothFlash' UNION ALLSELECT 26, 8, N'LedPairColour1Colour2Alternate' UNION ALLSELECT 26, 9, N'LedPairColour1BothAlternate' UNION ALLSELECT 26, 10, N'LedPairColour2BothAlternate' UNION ALLSELECT 27, 1, N'TemperatureStateNormal' UNION ALLSELECT 27, 2, N'TemperatureStateLow' UNION ALLSELECT 27, 3, N'TemperatureStateWarn' UNION ALLSELECT 27, 4, N'TemperatureStateHigh' UNION ALLSELECT 28, 1, N'TemperatureLowThresholdDown' UNION ALLSELECT 28, 2, N'TemperatureLowThresholdUp' UNION ALLSELECT 28, 3, N'TemperatureVeryLowThresholdDown' UNION ALLSELECT 28, 4, N'TemperatureVeryLowThresholdUp' UNION ALLSELECT 28, 5, N'TemperatureHighThresholdDown' UNION ALLSELECT 28, 6, N'TemperatureHighThresholdUp' UNION ALLSELECT 31, 1, N'BatteryCharging' UNION ALLSELECT 31, 2, N'BatteryFullyCharged' UNION ALLSELECT 31, 3, N'BatteryPoweringSystem' UNION ALLSELECT 31, 4, N'BatteryNotFitted'COMMIT;RAISERROR (N'[dbo].[Linxdatatypesvalues]: Loaded values', 10, 1) WITH NOWAIT;GO===================then (drum roll..........)When I run:-=========================SELECT 1 AS Tag, null AS Parent, types as [types!1!Types], h.typesid as [typesid!2!Typesid], H.name as [Name!2!Name], H.Description as [Description!2!h.Description], null, null FROM Linxdatatypes H Union all SELECT 2 AS Tag, 1 AS Parent, h.types, h.typesid, null, null, V.value as [V.Value!3!Value], V.Description as [V.Description!3!v.Description] FROM Linxdatatypes H, Linxdatatypesvalues V where H.typesid = V.typesid ORDER BY 3,4,7 for xml explicit============I get:-=========================================Msg 6812, Level 16, State 1, Line 2XML tag ID 2 that was originally declared as 'typesid' is being redeclared as 'Name'.============================================================Whats going wrong?Its driving me nuts....!!!but when I run it without the "for XML" line - it seems ok..ishTag Parent types!1!Types typesid!2!Typesid Name!2!Name Description!2!h.Description (No column name) (No column name)1 NULL EnumType 10 enumLightbarFlashingMode_t Lightbar settings| NULL NULL2 1 EnumType 10 NULL NULL 0 FlashFast2 1 EnumType 10 NULL NULL 1 FlashSlow1 NULL EnumType 11 enumButtonStatus_t Push button status| NULL NULL2 1 EnumType 11 NULL NULL 1 ButtonNotPushed2 1 EnumType 11 NULL NULL 2 ButtonPushed1 NULL EnumType 12 enumButtonPress_t Push button pushes| NULL NULL2 1 EnumType 12 NULL NULL 1 ButtonMomentaryPush2 1 EnumType 12 NULL NULL 2 ButtonPushAndHold2 1 EnumType 12 NULL NULL 3 ButtonRelease1 NULL EnumType 13 enumIgnitionSwitch_t State of ignition switch| NULL NULL2 1 EnumType 13 NULL NULL 1 IgnitionSwitchOff2 1 EnumType 13 NULL NULL 2 IgnitionSwitchOn1 NULL EnumType 14 enumVehicleBatteryState_t Vehicle battery state| NULL NULL2 1 EnumType 14 NULL NULL 1 VehicleBatteryStateNormal2 1 EnumType 14 NULL NULL 2 VehicleBatteryStateLow2 1 EnumType 14 NULL NULL 3 VehicleBatteryStateVeryLow2 1 EnumType 14 NULL NULL 4 VehicleBatteryStateHigh1 NULL EnumType 15 enumVehicleBatteryThreshold_t Threshold voltages for vehicle battery| NULL NULL2 1 EnumType 15 NULL NULL 1 VehicleBatteryLowThresholdDown2 1 EnumType 15 NULL NULL 2 VehicleBatteryLowThresholdUp2 1 EnumType 15 NULL NULL 3 VehicleBatteryVeryLowThresholdDown2 1 EnumType 15 NULL NULL 4 VehicleBatteryVeryLowThresholdUp2 1 EnumType 15 NULL NULL 5 VehicleBatteryHighThresholdDown2 1 EnumType 15 NULL NULL 6 VehicleBatteryHighThresholdUp1 NULL EnumType 16 enumLightbarAdvancedFlashingMode_t Lightbar settings| NULL NULL2 1 EnumType 16 NULL NULL 0 Flash1Hz2 1 EnumType 16 NULL NULL 1 Flash2Hz2 1 EnumType 16 NULL NULL 2 Flash4Hz2 1 EnumType 16 NULL NULL 3 Flash8Hz2 1 EnumType 16 NULL NULL 4 Flash16Hz1 NULL EnumType 17 enumDeviceState_t State of device| NULL NULL2 1 EnumType 17 NULL NULL 1 DevicePowerOff2 1 EnumType 17 NULL NULL 2 DeviceSwitchingOn2 1 EnumType 17 NULL NULL 3 DevicePowerOn2 1 EnumType 17 NULL NULL 4 DeviceSwitchingOff2 1 EnumType 17 NULL NULL 6 DeviceProhibited1 NULL EnumType 18 enumCameraFlashState_t Camera flash state| NULL NULL2 1 EnumType 18 NULL NULL 1 CameraFlashOff2 1 EnumType 18 NULL NULL 2 CameraFlashTorch2 1 EnumType 18 NULL NULL 3 CameraFlashPhoto1 NULL EnumType 19 enumDeviceRequiredState_t Required state of device| NULL NULL2 1 EnumType 19 NULL NULL 1 DeviceSetPowerOff2 1 EnumType 19 NULL NULL 3 DeviceSetPowerOn2 1 EnumType 19 NULL NULL 5 DeviceSetDefault2 1 EnumType 19 NULL NULL 6 DeviceSetProhibited1 NULL EnumType 20 enumDockLinxPresenceState_t LINX present in dock state| NULL NULL2 1 EnumType 20 NULL NULL 1 DockLinxAbsent2 1 EnumType 20 NULL NULL 2 DockLinxPresent1 NULL EnumType 21 enumDockLockStatus_t Status of docking station lock| NULL NULL2 1 EnumType 21 NULL NULL 1 DockLockLocked2 1 EnumType 21 NULL NULL 2 DockLockUnlocking2 1 EnumType 21 NULL NULL 3 DockLockUnlocked2 1 EnumType 21 NULL NULL 4 DockLockLocking1 NULL EnumType 22 enumDockStatus_t Status of docking| NULL NULL2 1 EnumType 22 NULL NULL 1 DockUndockRequestPending2 1 EnumType 22 NULL NULL 2 DockUndocking2 1 EnumType 22 NULL NULL 3 DockUndocked2 1 EnumType 22 NULL NULL 4 DockDockedVehicle2 1 EnumType 22 NULL NULL 5 DockDockedDeskstation2 1 EnumType 22 NULL NULL 6 DockDockedMobileOffice1 NULL EnumType 23 enumDockPowerStatus_t Power state in docking station| NULL NULL2 1 EnumType 23 NULL NULL 1 DockPowerOff2 1 EnumType 23 NULL NULL 2 DockPowerOn1 NULL EnumType 24 enumLedColour_t Colour of LED| NULL NULL2 1 EnumType 24 NULL NULL 1 LedRed2 1 EnumType 24 NULL NULL 2 LedYellow2 1 EnumType 24 NULL NULL 3 LedGreen2 1 EnumType 24 NULL NULL 4 LedBlue1 NULL EnumType 25 enumLedState_t On, off or flashing state of LED| NULL NULL2 1 EnumType 25 NULL NULL 1 LedOff2 1 EnumType 25 NULL NULL 2 LedOn2 1 EnumType 25 NULL NULL 5 LedFlash1 NULL EnumType 26 enumLedPairState_t On, off or flashing state of LED| NULL NULL2 1 EnumType 26 NULL NULL 1 LedPairBothOff2 1 EnumType 26 NULL NULL 2 LedPairColour12 1 EnumType 26 NULL NULL 3 LedPairColour22 1 EnumType 26 NULL NULL 4 LedPairBothOn2 1 EnumType 26 NULL NULL 5 LedPairColour1Flash2 1 EnumType 26 NULL NULL 6 LedPairColour2Flash2 1 EnumType 26 NULL NULL 7 LedPairBothFlash2 1 EnumType 26 NULL NULL 8 LedPairColour1Colour2Alternate2 1 EnumType 26 NULL NULL 9 LedPairColour1BothAlternate2 1 EnumType 26 NULL NULL 10 LedPairColour2BothAlternate1 NULL EnumType 27 enumTemperatureState_t LINX thermal state| NULL NULL2 1 EnumType 27 NULL NULL 1 TemperatureStateNormal2 1 EnumType 27 NULL NULL 2 TemperatureStateLow2 1 EnumType 27 NULL NULL 3 TemperatureStateWarn2 1 EnumType 27 NULL NULL 4 TemperatureStateHigh1 NULL EnumType 28 enumTemperatureThreshold_t Threshold temperatures for LINX| NULL NULL2 1 EnumType 28 NULL NULL 1 TemperatureLowThresholdDown2 1 EnumType 28 NULL NULL 2 TemperatureLowThresholdUp2 1 EnumType 28 NULL NULL 3 TemperatureVeryLowThresholdDown2 1 EnumType 28 NULL NULL 4 TemperatureVeryLowThresholdUp2 1 EnumType 28 NULL NULL 5 TemperatureHighThresholdDown2 1 EnumType 28 NULL NULL 6 TemperatureHighThresholdUp1 NULL EnumType 31 enumBatteryState_t Battery state| NULL NULL2 1 EnumType 31 NULL NULL 1 BatteryCharging2 1 EnumType 31 NULL NULL 2 BatteryFullyCharged2 1 EnumType 31 NULL NULL 3 BatteryPoweringSystem2 1 EnumType 31 NULL NULL 4 BatteryNotFitted1 NULL NumericType 0 U32_t Unsigned 32-bit value NULL NULL1 NULL NumericType 1 U16_t Unsigned 16-bit value NULL NULL1 NULL NumericType 2 U8_t Unsigned 8-bit value NULL NULL1 NULL NumericType 3 s32_t Signed 32-bit value NULL NULL1 NULL NumericType 4 s16_t Signed 16-bit value NULL NULL1 NULL NumericType 5 s8_t Signed 8-bit value NULL NULL1 NULL NumericType 6 boolean_t Boolean value NULL NULL1 NULL NumericType 7 extendedData_t Extended data pointer NULL NULL1 NULL NumericType 8 voltage_t Volts in tenths of a Volt NULL NULL1 NULL NumericType 9 test_t Test data type NULL NULL1 NULL NumericType 29 temperature_t Temperature in tenths of a degree Celsius NULL NULL1 NULL NumericType 30 batteryLevel_t Battery charge level percentage NULL NULL |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2009-05-28 : 21:16:07
|
First of all, have you tried FOR XML PATH? I'd really recommend it as it is much simpler than using FOR XML EXPLICIT and I have not yet had a case where I couldn't do what I needed with FOR XML PATH.You did a great job of putting everything into your question with the sample code and table and so on, unfortunately you left out one key thing - the structure of the XML you're trying to get to. So, I can tell you what you're doing wrong but I can't tell you how to get to exactly what you want without the XML structure.When you use FOR XML EXPLICIT, the column names determine the XML structure in this format TAGNAME!TAGID!ATTRIBUTENAME - so the problem with your code is that you are trying to use different names for the same tag (tag 2 in this case). You first name a column [typesid!2!Typesid] meaning that tag 2 is named typesid and then you name another column [Name!2!Name] meaning you are renaming tag 2 as Name.Also, you have to name columns in the first query when using a UNION, so you need to move all the names to the first query or you'll get more errors. Here's a working version of your query (it probably won't give the XML you want, but hopefully it will at least help you get further, and if you post the XML structure I can help you get there).SELECT 1 AS Tag, null AS Parent,types as [types!1!Types],h.typesid as [typesid!2!Typesid],H.name as [typesid!2!Name],H.Description as [typesid!2!h.Description],null as [V.Value!3!Value],null as [V.Value!3!v.Description]FROM Linxdatatypes H Union allSELECT 2, 1,h.types,h.typesid,null,null,V.value,V.Description FROM Linxdatatypes H, Linxdatatypesvalues V where H.typesid = V.typesid ORDER BY 3,4,7for xml explicit |
 |
|
|
NicJ
Starting Member
15 Posts |
Posted - 2009-05-29 : 04:10:17
|
| Thanks for the response, I tried to give someone a chance to replicate my problem.The actual XML I will be creating is far bigger and more complex with over 10 tables in all - however I want to prove the theory before making things too complex.I have examined many samples of the use of 'explicit' on the net and never spotted that the first element was a unique name for the entire tag level - that explains a lot....duh!!!I will now try the new format once I can get back onto the VPN server and try again.CheersNic |
 |
|
|
NicJ
Starting Member
15 Posts |
Posted - 2009-05-29 : 04:49:11
|
| Well the error message has gone - now I need the next bit of help you mentioned.This is the format I am failing to acheive:-<LinxDataTypes> <types:NumericType PK_LinxTypeID="0" Name="U32_t" BasicType="unsigned long"> <types:Description>Unsigned 32-bit value</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="1" Name="U16_t" BasicType="unsigned short"> <types:Description>Unsigned 16-bit value</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="2" Name="U8_t" BasicType="unsigned char"> <types:Description>Unsigned 8-bit value</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="3" Name="S32_t" BasicType="signed long"> <types:Description>Signed 32-bit value</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="4" Name="S16_t" BasicType="signed short"> <types:Description>Signed 16-bit value</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="5" Name="S8_t" BasicType="signed char"> <types:Description>Signed 8-bit value</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="6" Name="boolean_t" BasicType="unsigned long"> <types:Description>Boolean value</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="7" Name="extendedData_t" BasicType="unsigned char*"> <types:Description>Extended data pointer</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="8" Name="voltage_t" FK_Alias="0" DecimalPlaces="1"> <types:Description>Volts in tenths of a Volt</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="9" Name="test_t" FK_Alias="0"> <types:Description>Test data type</types:Description> </types:NumericType> <types:EnumType PK_LinxTypeID="10" Name="enumLightbarFlashingMode_t"> <types:Description>Lightbar settings</types:Description> <types:EnumValue PK_Value="0">FlashFast</types:EnumValue> <types:EnumValue PK_Value="1">FlashSlow</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="11" Name="enumButtonStatus_t"> <types:Description>Push button status</types:Description> <types:EnumValue PK_Value="1">ButtonNotPushed</types:EnumValue> <types:EnumValue PK_Value="2">ButtonPushed</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="12" Name="enumButtonPress_t"> <types:Description>Push button pushes</types:Description> <types:EnumValue PK_Value="1">ButtonMomentaryPush</types:EnumValue> <types:EnumValue PK_Value="2">ButtonPushAndHold</types:EnumValue> <types:EnumValue PK_Value="3">ButtonRelease</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="13" Name="enumIgnitionSwitch_t"> <types:Description>State of ignition switch</types:Description> <types:EnumValue PK_Value="1">IgnitionSwitchOff</types:EnumValue> <types:EnumValue PK_Value="2">IgnitionSwitchOn</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="14" Name="enumVehicleBatteryState_t"> <types:Description>Vehicle battery state</types:Description> <types:EnumValue PK_Value="1">VehicleBatteryStateNormal</types:EnumValue> <types:EnumValue PK_Value="2">VehicleBatteryStateLow</types:EnumValue> <types:EnumValue PK_Value="3">VehicleBatteryStateVeryLow</types:EnumValue> <types:EnumValue PK_Value="4">VehicleBatteryStateHigh</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="15" Name="enumVehicleBatteryThreshold_t"> <types:Description>Threshold voltages for vehicle battery</types:Description> <types:EnumValue PK_Value="1">VehicleBatteryLowThresholdDown</types:EnumValue> <types:EnumValue PK_Value="2">VehicleBatteryLowThresholdUp</types:EnumValue> <types:EnumValue PK_Value="3">VehicleBatteryVeryLowThresholdDown</types:EnumValue> <types:EnumValue PK_Value="4">VehicleBatteryVeryLowThresholdUp</types:EnumValue> <types:EnumValue PK_Value="5">VehicleBatteryHighThresholdDown</types:EnumValue> <types:EnumValue PK_Value="6">VehicleBatteryHighThresholdUp</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="16" Name="enumLightbarAdvancedFlashingMode_t"> <types:Description>Lightbar settings</types:Description> <types:EnumValue PK_Value="0">Flash1Hz</types:EnumValue> <types:EnumValue PK_Value="1">Flash2Hz</types:EnumValue> <types:EnumValue PK_Value="2">Flash4Hz</types:EnumValue> <types:EnumValue PK_Value="3">Flash8Hz</types:EnumValue> <types:EnumValue PK_Value="4">Flash16Hz</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="17" Name="enumDeviceState_t"> <types:Description>State of device</types:Description> <types:EnumValue PK_Value="1">DevicePowerOff</types:EnumValue> <types:EnumValue PK_Value="2">DeviceSwitchingOn</types:EnumValue> <types:EnumValue PK_Value="3">DevicePowerOn</types:EnumValue> <types:EnumValue PK_Value="4">DeviceSwitchingOff</types:EnumValue> <types:EnumValue PK_Value="6">DeviceProhibited</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="18" Name="enumCameraFlashState_t"> <types:Description>Camera flash state</types:Description> <types:EnumValue PK_Value="1">CameraFlashOff</types:EnumValue> <types:EnumValue PK_Value="2">CameraFlashTorch</types:EnumValue> <types:EnumValue PK_Value="3">CameraFlashPhoto</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="19" Name="enumDeviceRequiredState_t"> <types:Description>Required state of device</types:Description> <types:EnumValue PK_Value="1">DeviceSetPowerOff</types:EnumValue> <types:EnumValue PK_Value="3">DeviceSetPowerOn</types:EnumValue> <types:EnumValue PK_Value="5">DeviceSetDefault</types:EnumValue> <types:EnumValue PK_Value="6">DeviceSetProhibited</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="20" Name="enumDockLinxPresenceState_t"> <types:Description>LINX present in dock state</types:Description> <types:EnumValue PK_Value="1">DockLinxAbsent</types:EnumValue> <types:EnumValue PK_Value="2">DockLinxPresent</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="21" Name="enumDockLockStatus_t"> <types:Description>Status of docking station lock</types:Description> <types:EnumValue PK_Value="1">DockLockLocked</types:EnumValue> <types:EnumValue PK_Value="2">DockLockUnlocking</types:EnumValue> <types:EnumValue PK_Value="3">DockLockUnlocked</types:EnumValue> <types:EnumValue PK_Value="4">DockLockLocking</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="22" Name="enumDockStatus_t"> <types:Description>Status of docking</types:Description> <types:EnumValue PK_Value="1">DockUndockRequestPending</types:EnumValue> <types:EnumValue PK_Value="2">DockUndocking</types:EnumValue> <types:EnumValue PK_Value="3">DockUndocked</types:EnumValue> <types:EnumValue PK_Value="4">DockDockedVehicle</types:EnumValue> <types:EnumValue PK_Value="5">DockDockedDeskstation</types:EnumValue> <types:EnumValue PK_Value="6">DockDockedMobileOffice</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="23" Name="enumDockPowerStatus_t"> <types:Description>Power state in docking station</types:Description> <types:EnumValue PK_Value="1">DockPowerOff</types:EnumValue> <types:EnumValue PK_Value="2">DockPowerOn</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="24" Name="enumLedColour_t"> <types:Description>Colour of LED</types:Description> <types:EnumValue PK_Value="1">LedRed</types:EnumValue> <types:EnumValue PK_Value="2">LedYellow</types:EnumValue> <types:EnumValue PK_Value="3">LedGreen</types:EnumValue> <types:EnumValue PK_Value="4">LedBlue</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="25" Name="enumLedState_t"> <types:Description>On, off or flashing state of LED</types:Description> <types:EnumValue PK_Value="1">LedOff</types:EnumValue> <types:EnumValue PK_Value="2">LedOn</types:EnumValue> <types:EnumValue PK_Value="5">LedFlash</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="26" Name="enumLedPairState_t"> <types:Description>On, off or flashing state of LED</types:Description> <types:EnumValue PK_Value="1">LedPairBothOff</types:EnumValue> <types:EnumValue PK_Value="2">LedPairColour1</types:EnumValue> <types:EnumValue PK_Value="3">LedPairColour2</types:EnumValue> <types:EnumValue PK_Value="4">LedPairBothOn</types:EnumValue> <types:EnumValue PK_Value="5">LedPairColour1Flash</types:EnumValue> <types:EnumValue PK_Value="6">LedPairColour2Flash</types:EnumValue> <types:EnumValue PK_Value="7">LedPairBothFlash</types:EnumValue> <types:EnumValue PK_Value="8">LedPairColour1Colour2Alternate</types:EnumValue> <types:EnumValue PK_Value="9">LedPairColour1BothAlternate</types:EnumValue> <types:EnumValue PK_Value="10">LedPairColour2BothAlternate</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="27" Name="enumTemperatureState_t"> <types:Description>LINX thermal state</types:Description> <types:EnumValue PK_Value="1">TemperatureStateNormal</types:EnumValue> <types:EnumValue PK_Value="2">TemperatureStateLow</types:EnumValue> <types:EnumValue PK_Value="3">TemperatureStateWarn</types:EnumValue> <types:EnumValue PK_Value="4">TemperatureStateHigh</types:EnumValue> </types:EnumType> <types:EnumType PK_LinxTypeID="28" Name="enumTemperatureThreshold_t"> <types:Description>Threshold temperatures for LINX</types:Description> <types:EnumValue PK_Value="1">TemperatureLowThresholdDown</types:EnumValue> <types:EnumValue PK_Value="2">TemperatureLowThresholdUp</types:EnumValue> <types:EnumValue PK_Value="3">TemperatureVeryLowThresholdDown</types:EnumValue> <types:EnumValue PK_Value="4">TemperatureVeryLowThresholdUp</types:EnumValue> <types:EnumValue PK_Value="5">TemperatureHighThresholdDown</types:EnumValue> <types:EnumValue PK_Value="6">TemperatureHighThresholdUp</types:EnumValue> </types:EnumType> <types:NumericType PK_LinxTypeID="29" Name="temperature_t" FK_Alias="3" DecimalPlaces="1"> <types:Description>Temperature in tenths of a degree Celsius</types:Description> </types:NumericType> <types:NumericType PK_LinxTypeID="30" Name="batteryLevel_t" FK_Alias="0" DecimalPlaces="0"> <types:Description>Battery charge level percentage</types:Description> </types:NumericType> <types:EnumType PK_LinxTypeID="31" Name="enumBatteryState_t"> <types:Description>Battery state</types:Description> <types:EnumValue PK_Value="1">BatteryCharging</types:EnumValue> <types:EnumValue PK_Value="2">BatteryFullyCharged</types:EnumValue> <types:EnumValue PK_Value="3">BatteryPoweringSystem</types:EnumValue> <types:EnumValue PK_Value="4">BatteryNotFitted</types:EnumValue> </types:EnumType> </LinxDataTypes>Any helpwould be much appreciated - I am not new to sql server or SQL itself - but XML is a new black art for me I am afraid.CheersNic |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2009-05-30 : 01:48:57
|
You're using a namespace in the XML (the types:) prefix on the elements and I'm not even sure how you use namespaces with FOR EXPLICIT. You can put the types: prefix in the tag names but the actual namespace declarations don't come through. Any how FOR XML PATH supports namespaces directly and as I said before I find it much easier to work with.Here's a query that uses FOR XML PATH to give you pretty much what you're looking for:WITH XMLNAMESPACES ('uri' AS types)SELECT (SELECT h.typesid AS [@PK_LinxTypeID], H.name AS [@Name], H.Basictype AS [@BasicType], H.Description AS [types:Description] FROM Linxdatatypes H WHERE types = 'NumericType' FOR XML PATH('types:NumericType'), TYPE), (SELECT h.typesid AS [@PK_LinxTypeID], H.name AS [@Name], H.Basictype AS [@BasicType], H.Description AS [types:Description], (SELECT V.VALUE AS [@PK_Value], V.Description AS [text()] FROM Linxdatatypesvalues V WHERE H.typesid = V.typesid FOR XML PATH('types:EnumValue'), TYPE) FROM Linxdatatypes H WHERE types = 'EnumType' FOR XML PATH('types:EnumType'), TYPE)FOR XML PATH('LinxDataTypes') |
 |
|
|
NicJ
Starting Member
15 Posts |
Posted - 2009-06-01 : 05:48:32
|
| Thanks for that - the response was far more detailed than I had expected.I have not used XML in anger at all and I dont know if the inclusion of the extra:-xmlns:types="uri"in each line will cause a problem - I will ask the guys who are expecting the data.Now all I need to do is spend a few hours understanding how you acheived that result so I can replicate the technique for the other XML 'extractions'.CheersNic |
 |
|
|
|
|
|
|
|