create or replace view peppol_invoice_tax_representative_party_xv
as
select
    invoice_id
    ,xmlelement("cac:TaxRepresentativeParty",
        xmlforest(
            xmlforest(
                "PartyName_Name" as "cbc:Name"
            ) as "cac:PartyName"
            ,xmlforest(
                "PostalAddress_StreetName"  as "cbc:StreetName"
                ,"PostalAddress_AdditionalStreetName" as "cbc:AdditionalStreetName"
                ,"PostalAddress_CityName" as "cbc:CityName"
                ,"PostalAddress_PostalZone" as "cbc:PostalZone"
                ,"PostalAddress_CountrySubentity" as "cbc:CountrySubentity"
                ,xmlforest(
                    "PostalAddress_AddressLine_Line" as "cbc:Line"
                ) as "cac:AddressLine"
                ,xmlforest(
                    "PostalAddress_Country_IdentificationCode" as "cbc:IdentificationCode"
                ) as "cac:Country"
            ) as "cac:PostalAddress"
            ,xmlforest(
                "PartyTaxScheme_CompanyID" as "cbc:CompanyID"
                ,xmlforest(
                    "PartyTaxScheme_TaxScheme_ID" as "cbc:ID"
                ) as "cac:TaxScheme"
            ) as "cac:PartyTaxScheme"
        )
    ) as "TaxRepresentativeParty"
from peppol_invoice_tax_representative_party_v;