SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Options to assign a dynamic content to XMLNAMESPACES area


Options to assign a dynamic content to XMLNAMESPACES area

Author
Message
BOR15K
BOR15K
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4619 Visits: 585
I have a long running process, generating XML files with XML Namespaces, which are defined using WITH XMLNAMESPACES as below:


WITH XMLNAMESPACES (
'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2' AS [nl-cbc],
'urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2' AS udt,
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS cac,
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' AS cbc,
'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' AS ext,
'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS doc,
'http://www.w3.org/2001/XMLSchema-instance' AS xsi,
DEFAULT 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'
)



I have now a new request to add a leading Invoice or CreditNote respectively, so it will start like
'Invoice urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2

or
'CreditNote urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2


I can think of the following three options, but I wonder if there is something simpler, please.

Option 1
Have an IF statement and repeat the whole select statement of over 200 rows:

IF @is_it_credit_note = 'N'
WITH XMLNAMESPACES (
'Invoice urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2' AS [nl-cbc],
'urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2' AS udt,
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS cac,
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' AS cbc,
'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' AS ext,
'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS doc,
'http://www.w3.org/2001/XMLSchema-instance' AS xsi,
DEFAULT 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'
)
SET @v_xml = (SELECT....)

IF @is_it_credit_note = 'Y'
WITH XMLNAMESPACES (
'CreditNote urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2' AS [nl-cbc],
'urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2' AS udt,
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS cac,
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' AS cbc,
'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' AS ext,
'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS doc,
'http://www.w3.org/2001/XMLSchema-instance' AS xsi,
DEFAULT 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'
)
SET @v_xml = (SELECT....)


Option 2

To make the whole code dynamic and to control xmlnamespaces area via a variable

Option 3

to execute the whole statement and then to only replace the xmlnamespaces


DECLARE @v_xmlnamespace VARCHAR(100) = 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2',
@v_invoice_xmlnamespace VARCHAR(100) = 'Invoice urn:oasis:names:specification:ubl:schema:xsd:Invoice-2',
@v_credit_xmlnamespace VARCHAR(100) = 'CreditNote urn:oasis:names:specification:ubl:schema:xsd:Invoice-2',


WITH XMLNAMESPACES (
'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2' AS [nl-cbc],
'urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2' AS udt,
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS cac,
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' AS cbc,
'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' AS ext,
'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS doc,
'http://www.w3.org/2001/XMLSchema-instance' AS xsi,
DEFAULT 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'
)

SET @v_xml = (SELECT....)

SELECT CAST(REPLACE(@v_xml, @v_xmlnamespace,CASE @is_this_invoice WHEN 'Y' THEN @v_invoice_xmlnamespace ELSE @v_credit_xmlnamespace END) AS XML)


Currently I am going with the last option, but wonder if there is anything simpler, please?

drew.allen
drew.allen
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63803 Visits: 16968
Why don't you just have two separate namespaces? The whole reason for introducing namespaces in the first place was so that you could distinguish between similar elements from two different sources. Trying to cram two different sources into the same namespace defeats that purpose.

WITH XMLNAMESPACES (
'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2' AS [nl-credit],
'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS [nl-invoice],


Drew


J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
BOR15K
BOR15K
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4619 Visits: 585
drew.allen - Thursday, October 11, 2018 8:12 AM
Why don't you just have two separate namespaces? The whole reason for introducing namespaces in the first place was so that you could distinguish between similar elements from two different sources. Trying to cram two different sources into the same namespace defeats that purpose.

WITH XMLNAMESPACES (
'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2' AS [nl-credit],
'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS [nl-invoice],


Drew
Do you mean I can control [nl-credit] / [nl-invoice] logic?
Thank you.

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search