Pivot complex XML using Xquery - sql-server
Is it possible to pivot the following xml into the following result set, or get the structure as close to it as possible? It can obviously have more than 1 item with similar data, I have just trimmed it down so only item sku 987654 is in the file.
DECLARE #XML AS XML = '<data xsi:schemaLocation="http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex catalog.xsd http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt dt.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt" major="6" minor="1" family="enfinity" branch="enterprise" build="2.6.6-R-1.1.59.2-20210714.2">
<item sku="987654">
<sku>987654</sku>
<category-links>
<category-link name="abc" domain="WhiteStuff-DE-WebCategories" default = "0" hotdeal = "0"/>
<category-link name="def" domain="WhiteStuff-DE-WebCategories" default = "1" hotdeal = "0"/>
<category-link name="ghi" domain="WhiteStuff-DE-WebCategories" default = "0" hotdeal = "0"/>
</category-links>
<images>
<primary-view image-view="FF" />
<image-ref image-view="FD" image-type="w150" image-base-name="FD.jpg" domain="WhiteStuff" />
<image-ref image-view="FF" image-type="ORI" image-base-name="FF.jpg" domain="WhiteStuff" />
</images>
<variations>
<variation-attributes>
<variation-attribute name = "size">
<presentation-option>default</presentation-option>
<custom-attributes>
<custom-attribute name="displayName" dt:dt="string" xml:lang="en-US">Size</custom-attribute>
<custom-attribute name="productDetailUrl" xml:lang="de-DE" dt:dt="string">123.co.uk</custom-attribute>
</custom-attributes>
</variation-attribute>
<variation-attribute name = "colour">
<presentation-option>colorCode</presentation-option>
<presentation-product-attribute-name>rgbColour</presentation-product-attribute-name>
<custom-attributes>
<custom-attribute name="displayName" dt:dt="string" xml:lang="en-US">Colour</custom-attribute>
<custom-attribute name="productDetailUrl" xml:lang="de-DE" dt:dt="string">456.co.uk</custom-attribute>
</custom-attributes>
</variation-attribute>
</variation-attributes>
</variations>
</item>
</data>
'
This is my starting block:
;WITH XMLNAMESPACES
(
DEFAULT 'http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex',
'http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt' as dt
)
SELECT n.value('#sku', 'nvarchar(max)') as [sku]
--[category-link],
--[FD image],
--[FF image],
--[productDetailUrl DE],
--[productDetailUrl EN]
FROM #XML.nodes('/data/item') as x(n);
It is not so clear how to distinguish between languages:
[productDetailUrl DE]
[productDetailUrl EN]
Other than that, please try the following solution. It will get you started.
SQL
DECLARE #XML AS XML =
N'<?xml version="1.0"?>
<data xsi:schemaLocation="http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex catalog.xsd http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt dt.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt"
major="6" minor="1" family="enfinity" branch="enterprise"
build="2.6.6-R-1.1.59.2-20210714.2">
<item sku="987654">
<sku>987654</sku>
<category-links>
<category-link name="abc" domain="WhiteStuff-DE-WebCategories"
default="0" hotdeal="0"/>
<category-link name="def" domain="WhiteStuff-DE-WebCategories"
default="1" hotdeal="0"/>
<category-link name="ghi" domain="WhiteStuff-DE-WebCategories"
default="0" hotdeal="0"/>
</category-links>
<images>
<primary-view image-view="FF"/>
<image-ref image-view="FD" image-type="w150"
image-base-name="FD.jpg" domain="WhiteStuff"/>
<image-ref image-view="FF" image-type="ORI" image-base-name="FF.jpg"
domain="WhiteStuff"/>
</images>
<variations>
<variation-attributes>
<variation-attribute name="size">
<presentation-option>default</presentation-option>
<custom-attributes>
<custom-attribute name="displayName" dt:dt="string"
xml:lang="en-US">Size</custom-attribute>
<custom-attribute name="productDetailUrl"
xml:lang="de-DE" dt:dt="string">123.co.uk</custom-attribute>
</custom-attributes>
</variation-attribute>
<variation-attribute name="colour">
<presentation-option>colorCode</presentation-option>
<presentation-product-attribute-name>rgbColour</presentation-product-attribute-name>
<custom-attributes>
<custom-attribute name="displayName" dt:dt="string"
xml:lang="en-US">Colour</custom-attribute>
<custom-attribute name="productDetailUrl"
xml:lang="de-DE" dt:dt="string">456.co.uk</custom-attribute>
</custom-attributes>
</variation-attribute>
</variation-attributes>
</variations>
</item>
</data>';
;WITH XMLNAMESPACES
(
DEFAULT 'http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex',
'http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt' as dt
)
SELECT c.value('#sku', 'nvarchar(max)') as [sku]
, n.value('#name','VARCHAR(20)') AS [category-link]
, c.value('(images/image-ref[#image-view="FD"]/#image-base-name)[1]','VARCHAR(20)') AS [FD image]
, c.value('(images/image-ref[#image-view="FF"]/#image-base-name)[1]','VARCHAR(20)') AS [FF image]
, c.value('(variations/variation-attributes/variation-attribute/custom-attributes/custom-attribute[#xml:lang="de-DE"]/text())[1]','VARCHAR(20)') AS [productDetailUrl DE]
, c.value('(variations/variation-attributes/variation-attribute[#name="colour"]/custom-attributes/custom-attribute[#xml:lang="de-DE"]/text())[1]','VARCHAR(20)') AS [productDetailUrl EN]
FROM #XML.nodes('/data/item') as t(c)
CROSS APPLY t.c.nodes('category-links/category-link') AS t2(n);
Output
+--------+---------------+----------+----------+---------------------+---------------------+
| sku | category-link | FD image | FF image | productDetailUrl DE | productDetailUrl EN |
+--------+---------------+----------+----------+---------------------+---------------------+
| 987654 | abc | FD.jpg | FF.jpg | 123.co.uk | 456.co.uk |
| 987654 | def | FD.jpg | FF.jpg | 123.co.uk | 456.co.uk |
| 987654 | ghi | FD.jpg | FF.jpg | 123.co.uk | 456.co.uk |
+--------+---------------+----------+----------+---------------------+---------------------+
Related
type error cannot read properties of undefined
this is the function I declared const Cart = () => { const cart = useSelector((state) => state.cart); this is some of the codes within the function: <Info> {cart.products.map((product)=> ( <Product> <ProductDetail> <Image src={product.img}/> <Details> <ProductName> <b>Product :</b>{product.title} </ProductName> <ProductSize> <b>Size :</b>{product.size} </ProductSize> </Details> </ProductDetail> <PriceDetails> <ProductAmountContainer> <Remove/> <ProductAmount> {product.inStock} </ProductAmount> <Add/> </ProductAmountContainer> <ProductPrice> <b>RM </b>{product.price*product.quantity} </ProductPrice> </PriceDetails> </Product> ))} <Hr/> </Info> However , I am getting these two errors in my browser. TypeError: Cannot read properties of undefined (reading 'img') 155 | {cart.products.map((product)=> ( 156 | <Product> 157 | <ProductDetail> >158| <Image src={product.img}/> |159| ^ <Details> 160 | <ProductName> 161 | <b>Product :</b>{product.title} Another error: Cart C:/Users/timot/OneDrive/Desktop/html css projects/OmazonShop/frontend/src/pages/Cart.jsx:154 151 | <TopButton type = "filled">CHECKOUT NOW</TopButton> 152 | </Top> 153 | <Bottom> > 154 | <Info> | ^ 155 | {cart.products.map((product)=> ( 156 | <Product> 157 | <ProductDetail> I am a bit puzzled because my product.title is working just fine.
Return the Product Component if cart.products is available. You can use optional chaining or check if the products is available then return the Product otherwise return loading or any other text.
Read XML on SQL Server : Select custom attribute
I'm trying to read an XML, it works pretty well on the whole except for the attribute named : <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> which I am trying to get the value "1234567890" Here is the test code: DECLARE #XML XML = '<customers> <customer customer-no="00000001"> <credentials> <login>test#email.com</login> </credentials> <profile> <preferred-locale>fr_BE</preferred-locale> <custom-attributes> <custom-attribute attribute-id="ServiceId">1</custom-attribute> <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> </custom-attributes> </profile> <note/> </customer> <customer customer-no="00000002"> <credentials> <login>test2#email.com</login> </credentials> <profile> <preferred-locale>fr_FR</preferred-locale> <custom-attributes> <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> </custom-attributes> </profile> <note/> </customer> </customers>' SELECT CustomerNo = Events.value('#customer-no', 'int'), --EventType = Events.value('#Type', 'varchar(20)'), CustomerLogin =Events.value('(credentials/login)[1]', 'varchar(60)'), CustomerLocal =Events.value('(profile/preferred-locale)[1]', 'varchar(60)'), EventKind =Events.value('(profile/custom-attributes/custom-attribute)[2]', 'varchar(60)') FROM #XML.nodes('/customers/customer') AS XTbl(Events) The current result is: CustomerNo CustomerLogin CustomerLocal EventKind 1 test#email.com fr_BE 1234567890 2 test2#email.com fr_FR NULL And it's logical since custom-attributes are optional, so you can't access them with the index. So I'm looking for a way to access them by name: attribute-id="loyaltyNumber" Thanks,
..filter the path to the attribute which has attribute-id = “loyaltyNumber” EventKind =Events.value('(profile/custom-attributes/custom-attribute[#attribute-id="loyaltyNumber"])[1]', 'varchar(60)')
You can just add the attribute-id of the custom attribute to your query: DECLARE #XML XML = '<customers> <customer customer-no="00000001"> <credentials> <login>test#email.com</login> </credentials> <profile> <preferred-locale>fr_BE</preferred-locale> <custom-attributes> <custom-attribute attribute-id="ServiceId">1</custom-attribute> <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> </custom-attributes> </profile> <note/> </customer> <customer customer-no="00000002"> <credentials> <login>test2#email.com</login> </credentials> <profile> <preferred-locale>fr_FR</preferred-locale> <custom-attributes> <custom-attribute attribute-id="loyaltyNumber">1234567777</custom-attribute> </custom-attributes> </profile> <note/> </customer> </customers>' SELECT CustomerNo = Events.value('#customer-no', 'int'), --EventType = Events.value('#Type', 'varchar(20)'), CustomerLogin =Events.value('(credentials/login)[1]', 'varchar(60)'), CustomerLocal =Events.value('(profile/preferred-locale)[1]', 'varchar(60)'), EventKind =Events.value('(profile/custom-attributes/custom-attribute[#attribute-id = "loyaltyNumber"])[1]', 'varchar(60)') FROM #XML.nodes('/customers/customer') AS XTbl(Events)
SQL XML - Create a XML file from SQL Server for an invoice including invoice positions in one XML file
I create my xml file in this way (I do not show all output fields because there are very many fields): DECLARE #ID_Rechnung int = 8; WITH XMLNAMESPACES ( 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' as ext, 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' as cbc, 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' as cac, 'http://uri.etsi.org/01903/v1.3.2#' as xades, 'http://www.w3.org/2001/XMLSchema-instance' as xsi, 'http://www.w3.org/2000/09/xmldsig#' as ds ) SELECT #XMLData = xmldat.xmldataCol FROM ( SELECT ( SELECT -- HIER XML Daten generieren '' AS 'ext:UBLExtensions', '' AS 'ext:UBLExtensions/ext:UBLExtension', '' AS 'ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent', '2.1' AS 'cbc:UBLVersionID', 'TR1.2' AS 'cbc:CustomizationID', '' AS 'cbc:ProfileID', Rechnungen.Nummer AS 'cbc:ID', 'false' AS 'cbc:CopyIndicator', '' AS 'cbc:UUID', CAST(Rechnungen.Datum AS Date) AS 'cbc:IssueDate' FROM rechnungen WHERE rechnungen.id = #ID_Rechnung FOR XML PATH(''), ROOT('Invoice') ) AS xmldataCol This works fine - i get the following XML: <Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"> <ext:UBLExtensions> <ext:UBLExtension> <ext:ExtensionContent /> </ext:UBLExtension> </ext:UBLExtensions> <cbc:UBLVersionID>2.1</cbc:UBLVersionID> <cbc:CustomizationID>TR1.2</cbc:CustomizationID> <cbc:ProfileID /> <cbc:ID>R200001</cbc:ID> <cbc:CopyIndicator>false</cbc:CopyIndicator> <cbc:UUID /> <cbc:IssueDate>2020-06-29</cbc:IssueDate> </Invoice> But now i need the invoice positions in the same file. This SQL should be included in the first one and the date should be as invoice line in the xml file: SELECT Rechnungpos.ID AS 'cac:InvoiceLine/cbc:ID', Rechnungpos.Anzahl AS 'cac:InvoiceLine/cbc:InvoicedQuantity' FROM RechnungPos WHERE RechnungPos.id_Rechnung = #ID_Rechnung The output should be this: <Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"> <ext:UBLExtensions> <ext:UBLExtension> <ext:ExtensionContent /> </ext:UBLExtension> </ext:UBLExtensions> <cbc:UBLVersionID>2.1</cbc:UBLVersionID> <cbc:CustomizationID>TR1.2</cbc:CustomizationID> <cbc:ProfileID /> <cbc:ID>R200001</cbc:ID> <cbc:CopyIndicator>false</cbc:CopyIndicator> <cbc:UUID /> <cbc:IssueDate>2020-06-29</cbc:IssueDate> <cac:InvoiceLine> <cbc:ID>1<(cbc:> <cbc:InvoicedQuantity>3</cbc:InvoicedQuantity> </cac:InvoiceLine> <cac:InvoiceLine> <cbc:ID>5<(cbc:> <cbc:InvoicedQuantity>1</cbc:InvoicedQuantity> </cac:InvoiceLine> <cac:InvoiceLine> <cbc:ID>9<(cbc:> <cbc:InvoicedQuantity>2</cbc:InvoicedQuantity> </cac:InvoiceLine> </Invoice> Here is the Code to generate Test Data: CREATE TABLE [dbo].[Rechnungen]( [id] [int] NOT NULL, [Nummer] [nvarchar](20) NOT NULL, [Datum] [datetime] NOT NULL ) INSERT INTO Rechnungen (id, Nummer, Datum) VALUES (8, 'R200001', '29.06.2020') CREATE TABLE [dbo].Rechnungpos( [id] [int] NOT NULL, [id_Rechnung] [int] NOT NULL, [Anzahl] [float] NOT NULL ) INSERT INTO RechnungPos (id, id_Rechnung, Anzahl) VALUES (1, 8, 3) INSERT INTO RechnungPos (id, id_Rechnung, Anzahl) VALUES (5, 8, 1) INSERT INTO RechnungPos (id, id_Rechnung, Anzahl) VALUES (9, 8, 2) it has to run on different versions - my version is SQL Server 2019 How can i do that? Thanks for help, Thomas.
Here is how to do it. The only complexity is how to handle a child table and specially namespaces in the output XML. That's why XQuery and its FLWOR expression are in use. SQL -- DDL and sample data population, start DECLARE #Rechnungen TABLE (id int , Nummer nvarchar(20), Datum datetime); INSERT INTO #Rechnungen (id, Nummer, Datum) VALUES (8, 'R200001', '2020-06-29'); DECLARE #Rechnungpos TABLE (id int, id_Rechnung int, Anzahl float); INSERT INTO #RechnungPos (id, id_Rechnung, Anzahl) VALUES (1, 8, 3), (5, 8, 1), (9, 8, 2); -- DDL and sample data population, end DECLARE #ID_Rechnung int = 8; ;WITH XMLNAMESPACES ('urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' as ext , 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' as cbc , 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' as cac , 'http://uri.etsi.org/01903/v1.3.2#' as xades , 'http://www.w3.org/2001/XMLSchema-instance' as xsi , 'http://www.w3.org/2000/09/xmldsig#' as ds) SELECT ( SELECT '2.1' AS [cbc:UBLVersionID], 'TR1.2' AS [cbc:CustomizationID], '' AS [cbc:ProfileID], p.Nummer AS [cbc:ID], 'false' AS [cbc:CopyIndicator], '' AS [cbc:UUID], CAST(p.Datum AS Date) AS [cbc:IssueDate], ( SELECT c.id AS [cbc:ID] , CAST(c.Anzahl AS INT) AS [cbc:InvoicedQuantity] FROM #Rechnungpos AS c INNER JOIN #Rechnungen AS p ON p.id = c.id_Rechnung FOR XML PATH('r'), TYPE, ROOT('root') ) FROM #Rechnungen AS p WHERE p.id = #ID_Rechnung FOR XML PATH(''), TYPE, ROOT('Invoice') ).query('<Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"> <ext:UBLExtensions> <ext:UBLExtension> <ext:ExtensionContent/> </ext:UBLExtension> </ext:UBLExtensions> { for $x in /Invoice/*[local-name()!="root"] return $x, for $x in /Invoice/root/r return <cac:InvoiceLine>{$x/*}</cac:InvoiceLine> } </Invoice>'); Output <Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"> <ext:UBLExtensions> <ext:UBLExtension> <ext:ExtensionContent /> </ext:UBLExtension> </ext:UBLExtensions> <cbc:UBLVersionID>2.1</cbc:UBLVersionID> <cbc:CustomizationID>TR1.2</cbc:CustomizationID> <cbc:ProfileID /> <cbc:ID>R200001</cbc:ID> <cbc:CopyIndicator>false</cbc:CopyIndicator> <cbc:UUID /> <cbc:IssueDate>2020-06-29</cbc:IssueDate> <cac:InvoiceLine> <cbc:ID>1</cbc:ID> <cbc:InvoicedQuantity>3</cbc:InvoicedQuantity> </cac:InvoiceLine> <cac:InvoiceLine> <cbc:ID>5</cbc:ID> <cbc:InvoicedQuantity>1</cbc:InvoicedQuantity> </cac:InvoiceLine> <cac:InvoiceLine> <cbc:ID>9</cbc:ID> <cbc:InvoicedQuantity>2</cbc:InvoicedQuantity> </cac:InvoiceLine> </Invoice>
Thanks, Yitzhak Khabinsky for help - heres the complete solution: DECLARE #ID_Rechnung int = 8, #XMLData xml; WITH XMLNAMESPACES ('urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' as ext , 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' as cbc , 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' as cac , 'http://uri.etsi.org/01903/v1.3.2#' as xades , 'http://www.w3.org/2001/XMLSchema-instance' as xsi , 'http://www.w3.org/2000/09/xmldsig#' as ds) SELECT #XMLData = xmldat.xmldataCol FROM ( SELECT ( SELECT -- HIER XML Daten generieren '' AS 'ext:UBLExtensions', '' AS 'ext:UBLExtensions/ext:UBLExtension', '' AS 'ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent', '2.1' AS 'cbc:UBLVersionID', 'TR1.2' AS 'cbc:CustomizationID', '' AS 'cbc:ProfileID', Rechnungen.Nummer AS 'cbc:ID', 'false' AS 'cbc:CopyIndicator', '' AS 'cbc:UUID', CAST(Rechnungen.Datum AS Date) AS 'cbc:IssueDate', '' AS 'cbc:InvoiceTypeCode', Rechnungen.Bemerkung1 AS 'cbc:Note', #Waehrung AS 'cbc:DocumentCurrencyCode', #Waehrung AS 'cbc:TaxCurrencyCode', Rechnungen.Auftrag AS 'cac:OrderReference/cbc:ID', -- Verkaüfer '' AS 'cac:AccountingSupplierParty/cac:Party/cbc:EndpointID', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:PostalZone', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyId', 'VAT' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cac:TaxScheme/cbc:ID', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyLegalForm', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:Name', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:Telephone', '' AS 'cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail', -- Käufer '' AS 'cac:AccountingCustomerParty/cac:Party/cbc:EndpointID', Rechnungen.DebKreNr AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID', Rechnungen.DebBez01 + ' ' + DebBez02 AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyName/cbc:Name', Rechnungen.DebStrasse AS 'cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName', Rechnungen.DebOrt AS 'cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:CityName', Rechnungen.DebPLZ AS 'cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:PostalZone', Rechnungen.DebLandKFZ AS 'cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode', Rechnungen.DebUMSTID AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID', 'VAT' AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cac:TaxScheme/cbc:ID', Rechnungen.DebBez01 + ' ' + DebBez02 AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName', '' AS 'cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:Name', '' AS 'cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:Telephone', '' AS 'cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail', -- Kontoverbindung Verkäufer '' AS 'cac:PaymentMeans/cbc:PaymentMeansCode', '' AS 'cac:PaymentMeans/cac:PayeeFinancialAccount/cbc:ID', '' AS 'cac:PaymentMeans/cac:PayeeFinancialAccount/cbc:Name', '' AS 'cac:PaymentMeans/cac:PayeeFinancialAccount/cac:FinancialInstitutionBranch/cbc:ID', --'' AS 'cac:PaymentTerms/cbc:Note', -- Steuern #Waehrung AS 'cac:TaxTotal/cbc_TaxAmount/#currencyID', CAST(Rechnungen.BetragMWST AS nvarchar(15)) AS 'cac:TaxTotal/cbc_TaxAmount', #Waehrung AS 'cac:TaxTotal/cac:Taxubtotal/cbc:TaxableAmount/#currencyID', CAST(Rechnungen.BetragNetto AS nvarchar(15))AS 'cac:TaxTotal/cac:Taxubtotal/cbc:TaxableAmount', #Waehrung AS 'cac:TaxTotal/cac:Taxubtotal/cbc:TaxAmount/#currencyID', CAST(Rechnungen.BetragMWST AS nvarchar(15))AS 'cac:TaxTotal/cac:Taxubtotal/cbc:TaxAmount', '' AS 'cac:TaxTotal/cac:Taxubtotal/cac:TaxCategory/cbc:ID', CAST(Rechnungen.MWST AS nvarchar(2)) AS 'cac:TaxTotal/cac:Taxubtotal/cac:TaxCategory/cbc:Percent', 'VAT' AS 'cac:TaxTotal/cac:Taxubtotal/cac:TaxCategory/cac:TaxScheme/cbc:ID', #Waehrung AS 'cac:LegalMonetaryTotal/cbc:LineExtensionAmount/#currencyID', CAST(Rechnungen.BetragNetto AS nvarchar(15))AS 'cac:LegalMonetaryTotal/cbc:LineExtensionAmount', #Waehrung AS 'cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount/#currencyID', CAST(Rechnungen.BetragNetto AS nvarchar(15))AS 'cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount', #Waehrung AS 'cac:LegalMonetaryTotal/cbc:TaxInclusiveAmount/#currencyID', CAST(Rechnungen.BetragBrutto AS nvarchar(15))AS 'cac:LegalMonetaryTotal/cbc:TaxInclusiveAmount', #Waehrung AS 'cac:LegalMonetaryTotal/cbc:PayableAmount/#currencyID', CAST(Rechnungen.BetragBrutto AS nvarchar(15))AS 'cac:LegalMonetaryTotal/cbc:PayableAmount', ( SELECT Rechnungpos.id AS [cbc:ID] , CAST(Rechnungpos.Anzahl AS INT) AS [cbc:InvoicedQuantity] FROM Rechnungpos WHERE RechnungPos.id_Rechnung = #id_Rechnung FOR XML PATH('r'), TYPE, ROOT('root') ) FROM Rechnungen WHERE Rechnungen.id = #ID_Rechnung FOR XML PATH(''), TYPE, ROOT('Invoice') ) AS xmldataCol ) AS xmldat; SELECT #XMLData .query('<Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"> { for $x in /Invoice/*[local-name()!="root"] return $x, for $x in /Invoice/root/r return <cac:InvoiceLine>{$x/*}</cac:InvoiceLine> } </Invoice>');
Doubled tasks in Activiti process
I've got an Activiti code (Activiti 5.19 run with Camel on Servicemix) running in production and face some peculiar problem: sometimes I've got cases of doubled tasks in an execution (one of them is even tripled). It seems to have no clear pattern. What I've tested my code would throw an exception if any javascript call service to close the same task twice for some reason. The doubled tasks neither can be completed (resulting "UserTask should not be signalled before complete" exception) nor deleted because the tasks are part of the running execution. I cannot figure out what could cause such an effect and how to get rid of the redundant tasks in order to finish the damaged processes. Is there possible to just delete the later records (with the highest task id) in the act_ru_task? Anybody could help? <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <collaboration id="Collaboration"> <participant id="pool1" name="Pool" processRef="process_pool1"></participant> </collaboration> <process id="new_marketing" name="marketing" isExecutable="true" activiti:candidateStarterGroups="marketing,task_manager"> <laneSet id="laneSet_new_marketing"> <lane id="lane1" name="marketing"> <flowNodeRef>usertask1</flowNodeRef> <flowNodeRef>startevent1</flowNodeRef> <flowNodeRef>exclusivegateway1</flowNodeRef> <flowNodeRef>usertask2</flowNodeRef> <flowNodeRef>usertask3</flowNodeRef> <flowNodeRef>usertask4</flowNodeRef> <flowNodeRef>endevent1</flowNodeRef> <flowNodeRef>exclusivegateway2</flowNodeRef> <flowNodeRef>exclusivegateway3</flowNodeRef> <flowNodeRef>usertask5</flowNodeRef> <flowNodeRef>usertask6</flowNodeRef> <flowNodeRef>endevent2</flowNodeRef> </lane> </laneSet> <startEvent id="startevent1" name="Start"></startEvent> <userTask id="usertask1" name="Status_0" activiti:assignee="${employeeName}" activiti:candidateGroups="marketing" activiti:dueDate="${dueDate}"> <extensionElements> <activiti:formProperty id="actionsForm" name="actions" type="enum" writable="false"> <activiti:value id="cancel" name="cancel"></activiti:value> <activiti:value id="accepted" name="accepted"></activiti:value> <activiti:value id="wait" name="wait"></activiti:value> </activiti:formProperty> </extensionElements> </userTask> <sequenceFlow id="flow12" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway> <sequenceFlow id="flow1" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow> <userTask id="usertask2" name="Status_10" activiti:assignee="${employeeName}"> <extensionElements> <activiti:formProperty id="actionsForm" name="actions" type="enum" writable="false"> <activiti:value id="cancel" name="cancel"></activiti:value> <activiti:value id="proceed" name="proceed"></activiti:value> <activiti:value id="application" name="application"></activiti:value> <activiti:value id="accepted" name="accepted"></activiti:value> <activiti:value id="wait" name="wait"></activiti:value> </activiti:formProperty> </extensionElements> </userTask> <sequenceFlow id="flow2" sourceRef="exclusivegateway1" targetRef="usertask2"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'accepted'}]]></conditionExpression> </sequenceFlow> <userTask id="usertask3" name="Status_1" activiti:assignee="${employeeName}" activiti:dueDate="${dueDate}"></userTask> <sequenceFlow id="flow3" name="Wait" sourceRef="exclusivegateway1" targetRef="usertask3"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'wait'}]]></conditionExpression> </sequenceFlow> <sequenceFlow id="flow4" sourceRef="usertask3" targetRef="usertask1"></sequenceFlow> <userTask id="usertask4" name="Status_1000" activiti:assignee="${employeeName}"></userTask> <sequenceFlow id="flow5" name="GiveUp" sourceRef="exclusivegateway1" targetRef="usertask4"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'cancel'}]]></conditionExpression> </sequenceFlow> <endEvent id="endevent1" name="End"></endEvent> <sequenceFlow id="flow6" sourceRef="usertask4" targetRef="endevent1"></sequenceFlow> <exclusiveGateway id="exclusivegateway2" name="Exclusive Gateway"></exclusiveGateway> <sequenceFlow id="flow7" sourceRef="usertask2" targetRef="exclusivegateway2"></sequenceFlow> <endEvent id="endevent2" name="End"></endEvent> <sequenceFlow id="flow10" sourceRef="exclusivegateway2" targetRef="usertask2"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'wait'}]]></conditionExpression> </sequenceFlow> <sequenceFlow id="flow11" name="GiveUp" sourceRef="exclusivegateway2" targetRef="usertask4"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'cancel'}]]></conditionExpression> </sequenceFlow> <sequenceFlow id="flow8" sourceRef="exclusivegateway2" targetRef="usertask5"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'application'}]]></conditionExpression> </sequenceFlow> <sequenceFlow id="flow13" sourceRef="exclusivegateway2" targetRef="usertask6"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'proceed' || action == 'accepted'}]]></conditionExpression> </sequenceFlow> <userTask id="usertask5" name="Status_50" activiti:assignee="${employeeName}"> <extensionElements> <activiti:formProperty id="actionsForm" name="actions" type="enum" writable="false"> <activiti:value id="cancel" name="cancel"></activiti:value> <activiti:value id="accepted" name="accepted"></activiti:value> </activiti:formProperty> </extensionElements> </userTask> <sequenceFlow id="flow14" sourceRef="usertask5" targetRef="exclusivegateway3"></sequenceFlow> <exclusiveGateway id="exclusivegateway3" name="Exclusive Gateway"></exclusiveGateway> <sequenceFlow id="flow15" sourceRef="exclusivegateway3" targetRef="usertask6"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'accepted'}]]></conditionExpression> </sequenceFlow> <userTask id="usertask6" name="Status_90" activiti:assignee="${employeeName}"></userTask> <sequenceFlow id="flow17" sourceRef="usertask6" targetRef="endevent2"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'accepted'}]]></conditionExpression> </sequenceFlow> <sequenceFlow id="flow16" sourceRef="exclusivegateway3" targetRef="usertask4"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action == 'cancel'}]]></conditionExpression> </sequenceFlow> </process> </definitions> The problem was checked in Activiti database using the following query: select * from act_ru_task t0 where t0.execution_id_ in (select id_ from act_ru_execution t1 where (select count(*) from act_ru_task t2 where t1.id_ = t2.execution_id_) > 1); Query presented 30 cases like this, below 3 of them (user logins anonimised): id_ | rev_ | execution_id_ | proc_inst_id_ | proc_def_id_ | name_ | parent_task_id_ | description_ | task_def_key_ | owner_ | assignee_ |delegation_ | priority_ | create_time_ | 118399 | 1 | 118373 | 118373 | new_marketing:3:5004 | Status_50 | | | usertask5 | | auser | | 50 | 2017-08-08 08:46:28.771 | 118396 | 1 | 118373 | 118373 | new_marketing:3:5004 | Status_50 | | | usertask5 | | auser | | 50 | 2017-08-08 08:46:28.769 | 634792 | 1 | 453636 | 453636 | new_marketing:3:5004 | Status_1000 | | | usertask4 | | buser | | 50 | 2018-04-04 09:49:13.853 | 634791 | 1 | 453636 | 453636 | new_marketing:3:5004 | Status_1000 | | | usertask4 | | buser | | 50 | 2018-04-04 09:49:13.853 | 527864 | 1 | 527837 | 527837 | new_marketing:3:5004 | Status_90 | | | usertask6 | | cuser | | 50 | 2018-02-01 16:06:56.089 | 527867 | 1 | 527837 | 527837 | new_marketing:3:5004 | Status_90 | | | usertask6 | | cuser | | 50 | 2018-02-01 16:06:56.092 |
Import parent-child XML data to Sql Server Tables
I have a XML like this: <StateTree> <State ID="01"> <Name>State1</Name> <CityList> <City ID="01" Order="1" CityGroup="1" CityBuild="1" GeoLocation="X"> <Name>City1</Name> <Group>1</Group> <AreaList> <Area ID="01" GeoLocation="6"> <Name>Area1</Name> </Area> <Area ID="02" GeoLocation="6"> <Name>Area2</Name> </Area> </AreaList> </City> <City ID="02" Order="3" CityGroup="2" CityBuild="4" GeoLocation="5"> <Name>City2</Name> <Group>2</Group> <AreaList /> </City> </CityList> </State> </StateTree> and I want to convert it to tables like this: State: ID Name 01 State1 --------------------------------------------------- City: ID Order CityGroup CityBuild GeoLocation Name State1 01 1 1 1 X City1 01 02 3 2 4 5 City2 01 --------------------------------------------------- AreaList: ID GeoLocation Name CityID 01 6 Area1 01 02 6 Area2 01 How I can do this ? thanks
I'm not going to write this for every field and all the inserts, but the following SQL should point you in the right direction: declare #xml xml set #xml = ' <StateTree> <State ID="01"> <Name>State1</Name> <CityList> <City ID="01" Order="1" CityGroup="1" CityBuild="1" GeoLocation="X"> <Name>City1</Name> <Group>1</Group> <AreaList> <Area ID="01" GeoLocation="6"> <Name>Area1</Name> </Area> <Area ID="02" GeoLocation="6"> <Name>Area2</Name> </Area> </AreaList> </City> <City ID="02" Order="3" CityGroup="2" CityBuild="4" GeoLocation="5"> <Name>City2</Name> <Group>2</Group> <AreaList /> </City> </CityList> </State> </StateTree> ' --Select States select ID = s.value('#ID','varchar(10)'), Name = s.value('Name[1]','varchar(100)') from #xml.nodes('/StateTree/State') x(s) --Select Cities select ID = c.value('#ID','varchar(10)'), Name = c.value('Name[1]','varchar(100)'), StateID = c.value('../../#ID','varchar(10)') from #xml.nodes('/StateTree/State/CityList/City') x(c) --Select Areas select ID = a.value('#ID','varchar(10)'), Name = a.value('Name[1]','varchar(100)'), CityID = a.value('../../#ID','varchar(10)') from #xml.nodes('/StateTree/State/CityList/City/AreaList/Area') x(a)