Read XML on SQL Server : Select custom attribute - sql-server

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)

Related

Pivot complex XML using Xquery

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 |
+--------+---------------+----------+----------+---------------------+---------------------+

Liquibase and H2 problem with not existing table

I have a problem with liquibase and H2 database. I wanted to add some testing to my liquibase schema on push to github via github workflow. So I have decided to make a SpringBoot profile with H2 to make it work. And almost everything works. In two cases, I add constraints via sql and it gives me an exception telling that this table does not exist. When I comment those sql statements, then the whole thing works like a charm.
So, here is my yaml file for test profile:
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:task_schema;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;IGNORECASE=TRUE;INIT=CREATE SCHEMA IF NOT EXISTS TASK_SCHEMA
username: sa
password:
initialization-mode: always
liquibase:
default-schema: task_schema
drop-first: true
change-log: classpath:/database/changelogs/changelog-master.xml
autoconfigure:
exclude: org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
jpa:
database-platform: org.hibernate.dialect.H2Dialect
openInView: false
show_sql: true
generate-ddl: true
hibernate:
ddl-auto: create
properties:
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
changelog with interesting changes:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<include file="create-client-sequence.xml" relativeToChangelogFile="true"/>
<include file="create-client-table.xml" relativeToChangelogFile="true"/>
<include file="add-client-name-constraint.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
create-client-table.xml:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet author="liquibase" id="create-client-table">
<createTable tableName="CLIENT">
<column name="ID" type="${long.type}">
<constraints nullable="false"/>
</column>
<column name="UUID" type="UUID">
<constraints nullable="false"/>
</column>
<column name="FIRST_NAME" type="VARCHAR2(32 ${char.unit})">
<constraints nullable="true"/>
</column>
<column name="LAST_NAME" type="VARCHAR2(32 ${char.unit})">
<constraints nullable="true"/>
</column>
<column name="COMPANY_NAME" type="VARCHAR2(32 ${char.unit})">
<constraints nullable="true"/>
</column>
<column name="EMAIL" type="VARCHAR2(50 ${char.unit})"/>
<column name="NOTE" type="VARCHAR2(1000 ${char.unit})"/>
<column name="TELEPHONE" type="VARCHAR2(20 ${char.unit})"/>
<column name="PROJECTS_VALUE" type="${big_decimal.type}" defaultValueNumeric="0.0">
<constraints nullable="false"/>
</column>
<column name="CLIENT_TYPE" type="VARCHAR2(20 ${char.unit})">
<constraints nullable="false"/>
</column>
<column name="POST_CODE" type="VARCHAR2(6 ${char.unit})"/>
<column name="CITY" type="VARCHAR2(30 ${char.unit})"/>
<column name="STREET" type="VARCHAR2(30 ${char.unit})"/>
<column name="HOUSE_NUMBER" type="VARCHAR2(5 ${char.unit})"/>
<column name="FLAT_NUMBER" type="VARCHAR2(5 ${char.unit})"/>
</createTable>
<addPrimaryKey tableName="CLIENT" columnNames="ID" constraintName="PK_CLIENT"/>
</changeSet>
</databaseChangeLog>
and add-client-name-constraint.xml:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet author="liquibase" id="add-client-name-constraint">
<sql>
alter table CLIENT add constraint NAME_NOT_NULL
check
(
((CLIENT_TYPE = 'PRIVATE') and ((FIRST_NAME is NOT NULL) AND (LAST_NAME is NOT NULL))) or
((CLIENT_TYPE = 'CORPORATE') and (COMPANY_NAME is NOT NULL))
);
</sql>
</changeSet>
</databaseChangeLog>
and some part of stack trace (sorry for Polish there):
2021-06-07 20:49:29.122 INFO 1607585 --- [ main] l.e.j.JdbcExecutor : CREATE TABLE TASK_SCHEMA.CLIENT (ID BIGINT NOT NULL, UUID UUID NOT NULL, FIRST_NAME VARCHAR(32), LAST_NAME VARCHAR(32), COMPANY_NAME VARCHAR(32), EMAIL VARCHAR(50), NOTE VARCHAR(1000), TELEPHONE VARCHAR(20), PROJECTS_VALUE DECIMAL(19, 6) DEFAULT 0 NOT NULL, CLIENT_TYPE VARCHAR(20) NOT NULL, POST_CODE VARCHAR(6), CITY VARCHAR(30), STREET VARCHAR(30), HOUSE_NUMBER VARCHAR(5), FLAT_NUMBER VARCHAR(5))
2021-06-07 20:49:29.123 INFO 1607585 --- [ main] l.c.ChangeSet : Table CLIENT created
2021-06-07 20:49:29.123 INFO 1607585 --- [ main] l.e.j.JdbcExecutor : ALTER TABLE TASK_SCHEMA.CLIENT ADD CONSTRAINT PK_CLIENT PRIMARY KEY (ID)
2021-06-07 20:49:29.124 INFO 1607585 --- [ main] l.c.ChangeSet : Primary key added to CLIENT (ID)
2021-06-07 20:49:29.124 INFO 1607585 --- [ main] l.c.ChangeSet : ChangeSet classpath:/database/changelogs/v1.0.0/client/create-client-table.xml::create-client-table::liquibase ran successfully in 3ms
2021-06-07 20:49:29.124 INFO 1607585 --- [ main] l.e.j.JdbcExecutor : INSERT INTO TASK_SCHEMA.DATABASECHANGELOG (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, MD5SUM, DESCRIPTION, COMMENTS, EXECTYPE, CONTEXTS, LABELS, LIQUIBASE, DEPLOYMENT_ID) VALUES ('create-client-table', 'liquibase', 'classpath:/database/changelogs/v1.0.0/client/create-client-table.xml', NOW(), 4, '8:9f420f03a34211b142d310064019080e', 'createTable tableName=CLIENT; addPrimaryKey constraintName=PK_CLIENT, tableName=CLIENT', '', 'EXECUTED', NULL, NULL, '3.10.1', '3091768994')
2021-06-07 20:49:29.129 INFO 1607585 --- [ main] l.e.j.JdbcExecutor : alter table CLIENT add constraint NAME_NOT_NULL
check
(
((CLIENT_TYPE = 'PRIVATE') and ((FIRST_NAME is NOT NULL) AND (LAST_NAME is NOT NULL))) or
((CLIENT_TYPE = 'CORPORATE') and (COMPANY_NAME is NOT NULL))
)
2021-06-07 20:49:29.130 ERROR 1607585 --- [ main] l.c.ChangeSet : Change Set classpath:/database/changelogs/v1.0.0/client/add-client-name-constraint.xml::add-client-name-constraint::liquibase failed. Error: Tabela "CLIENT" nie istnieje
Table "CLIENT" not found; SQL statement:
alter table CLIENT add constraint NAME_NOT_NULL
check
(
((CLIENT_TYPE = 'PRIVATE') and ((FIRST_NAME is NOT NULL) AND (LAST_NAME is NOT NULL))) or
((CLIENT_TYPE = 'CORPORATE') and (COMPANY_NAME is NOT NULL))
) [42102-200] [Failed SQL: (42102) alter table CLIENT add constraint NAME_NOT_NULL
check
(
((CLIENT_TYPE = 'PRIVATE') and ((FIRST_NAME is NOT NULL) AND (LAST_NAME is NOT NULL))) or
((CLIENT_TYPE = 'CORPORATE') and (COMPANY_NAME is NOT NULL))
)]
2021-06-07 20:49:29.131 INFO 1607585 --- [ main] l.l.StandardLockService : Successfully released change log lock
2021-06-07 20:49:29.132 WARN 1607585 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set classpath:/database/changelogs/v1.0.0/client/add-client-name-constrain.xml::add-client-name-constraint::liquibase:
Reason: liquibase.exception.DatabaseException: Tabela "CLIENT" nie istnieje
Table "CLIENT" not found; SQL statement:
alter table CLIENT add constraint NAME_NOT_NULL
check
(
((CLIENT_TYPE = 'PRIVATE') and ((FIRST_NAME is NOT NULL) AND (LAST_NAME is NOT NULL))) or
((CLIENT_TYPE = 'CORPORATE') and (COMPANY_NAME is NOT NULL))
) [42102-200] [Failed SQL: (42102) alter table CLIENT add constraint NAME_NOT_NULL
check
(
((CLIENT_TYPE = 'PRIVATE') and ((FIRST_NAME is NOT NULL) AND (LAST_NAME is NOT NULL))) or
((CLIENT_TYPE = 'CORPORATE') and (COMPANY_NAME is NOT NULL))
)]
2021-06-07 20:49:29.133 INFO 1607585 --- [ main] o.s.s.c.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2021-06-07 20:49:29.133 INFO 1607585 --- [ main] c.z.h.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-06-07 20:49:29.136 INFO 1607585 --- [ main] c.z.h.HikariDataSource : HikariPool-1 - Shutdown completed.
2021-06-07 20:49:29.142 INFO 1607585 --- [ main] ConditionEvaluationReportLoggingListener :
I will appreciate any help.
EDIT:
Solved. For h2 SQL I have added schema name before table name and it works. The strange part of it is, that if I run H2 in console, it does not require that. So now, the file looks like this:
and add-client-name-constraint.xml:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet author="liquibase" id="add-client-name-constraint">
<sql dbms="postgresql">
alter table CLIENT add constraint NAME_NOT_NULL
check
(
((CLIENT_TYPE = 'PRIVATE') and ((FIRST_NAME is NOT NULL) AND (LAST_NAME is NOT NULL))) or
((CLIENT_TYPE = 'CORPORATE') and (COMPANY_NAME is NOT NULL))
);
</sql>
<sql dbms="h2">
alter table TASK_SCHEMA.CLIENT add constraint NAME_NOT_NULL
check
(
((CLIENT_TYPE = 'PRIVATE') and ((FIRST_NAME is NOT NULL) AND (LAST_NAME is NOT NULL))) or
((CLIENT_TYPE = 'CORPORATE') and (COMPANY_NAME is NOT NULL))
);
</sql>
</changeSet>
</databaseChangeLog>

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>');

Powershell: Copy Elements from one array to another

Hi in my first array there are my nodes from a xml.
$result=Select-xml -xml $uar -xpath "//test:UAVariable[contains(#NodeId,'ns=1;s=::')][starts-with(#DataType,'i=')]" -namespace $ns | select -ExpandProperty node
now i have to filter this further more like this. And then if both if´s are true delete the element from my array if its possible or copy this Element to a new one.
$result | foreach {
$AttExists = $_.Arraydimensions
$NodeExists = $_.References.Reference.ReferenceType
if ($AttExists){
if ($NodeExists -eq 'HasComponent'){
#if this is happening i want to delete this hole Element (Node) in my array is this possible?
}
else{
#if deleting is not possible i want to copy these Element (Node) into a new array is this possible?
}
}
}
Tried several things like:
$_=$nullor whith a counter like $newArray[$i] = $result[$j] $i++
But it dont delete anything or copy it. Can someone help me?
xml data :
<?xml version="1.0" encoding="utf-8"?>
<UANodeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns:ua="http://xxx/NodeSet.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pv="http://yyy/NodeSet.xsd" xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd">
<UAObject NodeId="ns=1;s=::" BrowseName="1:::">
<DisplayName><Default></DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=2;i=10001</Reference>
<Reference ReferenceType="Organizes" IsForward="false">ns=2;i=20001</Reference>
<Reference ReferenceType="Organizes">ns=1;s=::AsGlobalPV</Reference>
<Reference ReferenceType="Organizes">ns=1;s=::Program</Reference>
</References>
<Extensions>
<Extension>
<pv:ObjectExtension>
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
</pv:ObjectExtension>
</Extension>
</Extensions>
</UAObject>
<UAObject ParentNodeId="ns=1;s=::" NodeId="ns=1;s=::AsGlobalPV" BrowseName="1:Global PV">
<DisplayName>Global PV</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=61</Reference>
<Reference ReferenceType="Organizes" IsForward="false">ns=1;s=::</Reference>
<Reference ReferenceType="Organizes">ns=1;s=::AsGlobalPV:gFahrzeug</Reference>
<Reference ReferenceType="Organizes">ns=1;s=::AsGlobalPV:gLebewesen</Reference>
</References>
</UAObject>
<UAVariable DataType="ns=1;i=100000" ParentNodeId="ns=1;s=::AsGlobalPV" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug" BrowseName="1:gFahrzeug" AccessLevel="3" ValueRank="-1">
<DisplayName>gFahrzeug</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=1;i=100005</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gFahrzeug"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="ns=1;i=100010" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug" BrowseName="1:Kraftfahrzeug" AccessLevel="3" ValueRank="-1">
<DisplayName>Kraftfahrzeug</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=1;i=100015</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.PKW</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=3" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW" BrowseName="1:LKW" AccessLevel="3" ValueRank="1" ArrayDimensions="2">
<DisplayName>LKW</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[0]</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[1]</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.LKW"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=3" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[0]" BrowseName="1:LKW[0]" AccessLevel="3">
<DisplayName>LKW[0]</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.LKW[0]"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=3" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[1]" BrowseName="1:LKW[1]" AccessLevel="3">
<DisplayName>LKW[1]</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.LKW[1]"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
</UANodeSet>
Code:
[xml]$uar = Get-Content -Path 'C:\Users\strobel.ma\OneDrive - GEA\Desktop\XML to String\OpcUaMap(3)_28_07.uar'
$ns = New-Object System.Xml.XmlNamespaceManager($uar.NameTable) #asdf
$ns=#{GEA="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd";
ua="http://br-automation.com/OpcUa/configuration/NodeSet.xsd";
xsi="http://www.w3.org/2001/XMLSchema-instance";
uax="http://opcfoundation.org/UA/2008/02/Types.xsd";
xsd="http://www.w3.org/2001/XMLSchema";
pv="http://br-automation.com/OpcUa/PLC/PV.xsd"}
$result=Select-xml -xml $uar -xpath "//GEA:UAVariable[contains(#NodeId,'ns=1;s=::')][starts-with(#DataType,'i=')]" -namespace $ns | select -ExpandProperty node
$result | foreach {$_.NodeId = $_.NodeId -replace 'ns=1;s=::AsGlobalPV:'}
$result | foreach {$_.NodeId = $_.NodeId -replace 'ns=1;s=::'}
$result | foreach {
$AttExists = $_.Arraydimensions
$NodeExists = $_.References.Reference.ReferenceType
if ($AttExists){
if ($NodeExists -eq 'HasComponent'){
#if this is happening i want to delete this hole Element (Node) in my array is this possible?
}
else{
#if deleting is not possible i want to copy these Element (Node) into a new array is this possible?
}
}
}
I would suggest you a naive approach and just create a new $result2 object where you store exactly what you need
I guess the default System.Array type should be good enough:
declare the new variable where to store items:
$result2 = #()
and store in it whatever you want by using in your foreach loop the following:
$result2 += $_

Salesforce data loader import error: Unable to run process accountUpsert "java.lang.RuntimeException: java.lang.NullPointerException"

I am having problem with Bulk Import to salesforce. My code and each and everything is working fine for 5000 records but i need to upsert 100,000 records.
Please Help.
Below is my error log
Creating Map
2016-08-22 12:44:12,675 FATAL [main] process.ProcessRunner topLevelError (Proce
sRunner.java:238) - Unable to run process accountUpsert
java.lang.RuntimeException: java.lang.NullPointerException
at com.salesforce.dataloader.process.ProcessRunner.run(ProcessRunner.ja
a:162)
at com.salesforce.dataloader.process.ProcessRunner.run(ProcessRunner.ja
a:100)
at com.salesforce.dataloader.process.ProcessRunner.main(ProcessRunner.j
va:253)
Caused by: java.lang.NullPointerException
at java.lang.String$CaseInsensitiveComparator.compare(Unknown Source)
at java.lang.String$CaseInsensitiveComparator.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at java.util.AbstractCollection.addAll(Unknown Source)
at java.util.TreeSet.addAll(Unknown Source)
at com.salesforce.dataloader.mapping.Mapper.<init>(Mapper.java:87)
at com.salesforce.dataloader.mapping.LoadMapper.<init>(LoadMapper.java:
1)
at com.salesforce.dataloader.controller.Controller.createMapper(Control
er.java:195)
at com.salesforce.dataloader.process.ProcessRunner.run(ProcessRunner.ja
a:146)
... 2 more
process-conf.xml file
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="accountUpsert"
class="com.salesforce.dataloader.process.ProcessRunner"
singleton="false">
<description>accountInsert job gets the account record from the CSV file
and inserts it into Salesforce.</description>
<property name="name" value="accountUpsert"/>
<property name="configOverrideMap">
<map>
<entry key="sfdc.debugMessages" value="true"/>
<entry key="sfdc.debugMessagesFile"
value="D:\Yardi\Account Upload\accountInsertSoapTrace.log"/>
<entry key="sfdc.endpoint" value="https://test.salesforce.com/"/>
<entry key="sfdc.username" value="wwaqar.yar#graymath.com.stage"/>
<!--Password below has been encrypted using key file,
therefore, it will not work without the key setting:
process.encryptionKeyFile.
The password is not a valid encrypted value,
please generate the real value using the encrypt.bat utility -->
<entry key="sfdc.password" value="5ae9e1fa9b8d55afb228898dd3ec9bb94f9d19047cb28660831286e35db82743cf488132b0ce32a0"/>
<entry key="process.encryptionKeyFile" value="D:\Yardi\Account Upload\keytext.txt"/>
<entry key="sfdc.timeoutSecs" value="600"/>
<entry key="sfdc.loadBatchSize" value="200"/>
<entry key="sfdc.entity" value="Account"/>
<entry key="process.operation" value="upsert"/>
<entry key="sfdc.externalIdField" value="Applicant_ID__c" />
<entry key="process.mappingFile" value="D:\Yardi\Account Upload\accountSunMapping.sdl"/>
<entry key="dataAccess.name"
value="D:\Yardi\Account Upload\All Applicants from 1-1-2014 to 8-17-2016 10000.csv"/>
<entry key="process.outputSuccess"
value="D:\Yardi\Account Upload\accountInsert_success.csv"/>
<entry key="process.outputError"
value="D:\Yardi\Account Upload\accountInsert_error.csv"/>
<entry key="dataAccess.type" value="csvRead"/>
<entry key="process.initialLastRunDate"
value="2005-12-01T00:00:00.000-0800"/>
</map>
</property>
</bean>
</beans>
Below is my trigger which i am using to populate some customized values before insert or update.
trigger MapStatusToStageBeforeOpportunintyCreation on Opportunity (before insert, before update) {
Set<ID> acctIDS = new Set<ID>();
Set<ID> propIDS = new Set<ID>();
String tempOppName;
for (Opportunity o : Trigger.New){
if(o.AccountId != null){
acctIDS.add(o.AccountID);
}
}
for (Opportunity o : Trigger.New){
if(o.Property_Name__c != null){
propIDS.add(o.Property_Name__c);
}
}
Map<ID, Account> acctMap = new Map<ID, Account>([Select LastName From Account Where ID =: acctIDS]);
Map<ID, Property__c> propMap = new Map<ID, Property__c>([Select Property_Code__c From Property__c Where ID =: propIDS]);
for (Opportunity o : Trigger.New){
// Mapping the StageName with Application_Status__c before Insert/update
if (o.Application_Status__c == 'Denied'){
o.StageName ='Denied';
}
else if(o.Application_Status__c == 'Approved'){
o.StageName ='Approved - Conditions Pending';
}
else if(o.Application_Status__c == 'Expired'){
o.StageName ='Expired';
}
else if(o.Application_Status__c == 'Closed'){
o.StageName ='Closed Won';
}
else if(o.Application_Status__c == 'Cancelled'){
o.StageName ='Cancelled';
}
else if(o.Application_Status__c == 'DocsSent'){
o.StageName ='Approved - Final';
}
else if(o.Application_Status__c == 'Not Submitted'){
o.StageName ='EXCLUDE FROM FILE';
}
else if(o.Application_Status__c == 'Pending'){
o.StageName ='Application Submitted';
}
else if(o.Application_Status__c == 'Submitted'){
o.StageName ='Application Submitted';
}
else if(o.Application_Status__c == 'Unprocessed'){
o.StageName ='Application Submitted';
}
else if(o.Application_Status__c == 'Null'){
o.StageName ='Error';
}
System.debug('Opp trigger is fired. ');
System.debug(o);
/*Date appSubmittedDate = o.Application_Submitted__c;
String strAppSubmittedDate = appSubmittedDate.month()+'-'+appSubmittedDate.day()+'-'+appSubmittedDate.year();
Property__c prop = [Select Property_Code__c From Property__c Where id =:o.Property_Name__c]; //Property_Name__c is returning id of related Property that's why used it's value to find Property Code.
Account acc = [Select LastName From Account Where id =:o.AccountId]; //AccountId is returning id of related Account that's why used it's value to find Account's LastName.
//o.Name = strAppSubmittedDate;
o.Name =acc.LastName+'-'+prop.Property_Code__c+'-'+strAppSubmittedDate;
//o.Name =acc.LastName+'-'+prop.Property_Code__c+'-'+String.valueOf(o.Application_Submitted__c);
//*/
for(ID acctID :acctMap.keySet()){
if(o.AccountID == acctID){
tempOppName = acctMap.get(acctID).LastName;
}
}
for(ID propID :propMap.keySet()){
if(o.Property_Name__c == propID){
tempOppName =tempOppName + '-' + propMap.get(propID).Property_Code__c;
}
}
Date appSubmittedDate = o.Application_Submitted__c;
String strAppSubmittedDate = appSubmittedDate.month()+'-'+appSubmittedDate.day()+'-'+appSubmittedDate.year();
o.Name = tempOppName + '-' + strAppSubmittedDate;
Id trigRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Sun Homes - BDS').getRecordTypeId();
o.RecordTypeId = trigRecordTypeId;
}
}
Thanks in Advance

Resources