XSLT - Reading namespaces and map data

Here is my XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns9:Invoice
    xmlns:ns2="http://tradeshift.com/api/public/1.0"
    xmlns:ns3="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
    xmlns:ns4="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
    xmlns:ns5="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:ns6="http://tradeshift.com/api/1.0"
    xmlns:ns7="urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2"
    xmlns:ns8="urn:oasis:names:specification:ubl:schema:xsd:OrderChange-2"
    xmlns:ns9="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
    xmlns:ns10="urn:oasis:names:specification:ubl:schema:xsd:Quotation-2"
    xmlns:ns11="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"
    xmlns:ns12="urn:oasis:names:specification:ubl:schema:xsd:Order-2"
    xmlns:ns13="urn:oasis:names:specification:ubl:schema:xsd:Reminder-2"
    xmlns:ns14="urn:oasis:names:specification:ubl:schema:xsd:RemittanceAdvice-2"
    xmlns:ns15="urn:oasis:names:specification:ubl:schema:xsd:ReceiptAdvice-2"
    xmlns:ns16="urn:oasis:names:specification:ubl:schema:xsd:OrderCancellation-2"
    xmlns:ns17="urn:oasis:names:specification:ubl:schema:xsd:RequestForQuotation-2"
    xmlns:ns18="urn:oasis:names:specification:ubl:schema:xsd:OrderResponseSimple-2">
 
    <ns3:UBLVersionID>2.0</ns3:UBLVersionID>
    <ns3:CustomizationID>urn:tradeshift.com:ubl-2.0-customizations:2010-06</ns3:CustomizationID>
    <ns3:ProfileID schemeID="CWA 16073:2010" schemeAgencyID="CEN/ISSS WS/BII" schemeVersionID="1">urn:www.cenbii.eu:profile:bii04:ver1.0</ns3:ProfileID>
    <ns3:ID>AMF-0001</ns3:ID>
    <ns3:IssueDate>2013-09-04</ns3:IssueDate>
    <ns3:InvoiceTypeCode listID="UN/ECE 1001 Subset" listAgencyID="6" listVersionID="D08B">380</ns3:InvoiceTypeCode>
    <ns3:DocumentCurrencyCode>USD</ns3:DocumentCurrencyCode>
    <ns5:AdditionalDocumentReference>
        <ns3:ID>X</ns3:ID>
        <ns3:DocumentTypeCode listID="urn:tradeshift.com:api:1.0:documenttypecode">BOL ID</ns3:DocumentTypeCode>
    </ns5:AdditionalDocumentReference>
    <ns5:AdditionalDocumentReference>
        <ns3:ID>HALF_EVEN</ns3:ID>
        <ns3:DocumentTypeCode listID="urn:tradeshift.com:api:1.0:documenttypecode">RoundingRule</ns3:DocumentTypeCode>
    </ns5:AdditionalDocumentReference>
    <ns5:AdditionalDocumentReference>
        <ns3:ID>1</ns3:ID>
        <ns3:DocumentTypeCode listID="urn:tradeshift.com:api:1.0:documenttypecode">humanreadableversion</ns3:DocumentTypeCode>
        <ns5:Attachment>        </ns5:Attachment>
    </ns5:AdditionalDocumentReference>
    <ns5:AccountingSupplierParty>
        <ns5:Party>
            <ns5:PartyIdentification>
                <ns3:ID schemeID="US:EIN">14-5554443</ns3:ID>
            </ns5:PartyIdentification>
            <ns5:PartyName>
                <ns3:Name>CBRE</ns3:Name>
            </ns5:PartyName>
            <ns5:PostalAddress>
                <ns3:AddressFormatCode listID="UN/ECE 3477" listAgencyID="6" listVersionID="D08B">5</ns3:AddressFormatCode>
                <ns3:StreetName>Santa Monica Blvd.</ns3:StreetName>
                <ns3:AdditionalStreetName></ns3:AdditionalStreetName>
                <ns3:BuildingNumber>11150 </ns3:BuildingNumber>
                <ns3:MarkAttention></ns3:MarkAttention>
                <ns3:CityName>Los Angeles</ns3:CityName>
                <ns3:PostalZone>90025¿¿</ns3:PostalZone>
                <ns3:CountrySubentity>CA</ns3:CountrySubentity>
                <ns5:Country>
                    <ns3:IdentificationCode>US</ns3:IdentificationCode>
                </ns5:Country>
            </ns5:PostalAddress>
            <ns5:PartyTaxScheme>
                <ns3:CompanyID schemeID="US:EIN">14-5554443</ns3:CompanyID>
                <ns5:TaxScheme>
                    <ns3:Name>VAT</ns3:Name>
                </ns5:TaxScheme>
            </ns5:PartyTaxScheme>
            <ns5:PartyLegalEntity>
                <ns3:RegistrationName>CBRE</ns3:RegistrationName>
                <ns3:CompanyID schemeID="US:EIN">14-5554443</ns3:CompanyID>
            </ns5:PartyLegalEntity>
            <ns5:Contact>
                <ns3:ID schemeURI="http://tradeshift.com/api/1.0/userId">5df6b269-f7c5-457b-86e1-7ba8e0c63295</ns3:ID>
                <ns3:Name>Paula Allen</ns3:Name>
            </ns5:Contact>
            <ns5:Person>
                <ns3:FirstName>Paula</ns3:FirstName>
                <ns3:FamilyName>Allen</ns3:FamilyName>
            </ns5:Person>
        </ns5:Party>
    </ns5:AccountingSupplierParty>
    <ns5:AccountingCustomerParty>
        <ns3:CustomerAssignedAccountID>C920009</ns3:CustomerAssignedAccountID>
        <ns5:Party>
            <ns5:PartyIdentification>
                <ns3:ID schemeID="US:EIN">12-5556667</ns3:ID>
            </ns5:PartyIdentification>
            <ns5:PartyName>
                <ns3:Name>The IT Guy</ns3:Name>
            </ns5:PartyName>
            <ns5:PostalAddress>
                <ns3:AddressFormatCode listID="UN/ECE 3477" listAgencyID="6" listVersionID="D08B">5</ns3:AddressFormatCode>
                <ns3:StreetName>West Street</ns3:StreetName>
                <ns3:AdditionalStreetName></ns3:AdditionalStreetName>
                <ns3:BuildingNumber>76</ns3:BuildingNumber>
                <ns3:MarkAttention></ns3:MarkAttention>
                <ns3:CityName>New York</ns3:CityName>
                <ns3:PostalZone>87873</ns3:PostalZone>
                <ns3:CountrySubentity>NY</ns3:CountrySubentity>
                <ns5:Country>
                    <ns3:IdentificationCode>US</ns3:IdentificationCode>
                </ns5:Country>
            </ns5:PostalAddress>
            <ns5:PartyTaxScheme>
                <ns3:CompanyID schemeID="US:EIN">12-5556667</ns3:CompanyID>
                <ns5:TaxScheme>
                    <ns3:Name>VAT</ns3:Name>
                </ns5:TaxScheme>
            </ns5:PartyTaxScheme>
            <ns5:PartyLegalEntity>
                <ns3:RegistrationName>The IT Guy</ns3:RegistrationName>
                <ns3:CompanyID schemeID="US:EIN">12-5556667</ns3:CompanyID>
            </ns5:PartyLegalEntity>
            <ns5:Contact/>
        </ns5:Party>
    </ns5:AccountingCustomerParty>
    <ns5:Delivery>
        <ns3:ActualDeliveryDate>2013-09-05</ns3:ActualDeliveryDate>
    </ns5:Delivery>
    <ns5:PaymentMeans>
        <ns3:ID>08886d2d-46ad-4516-a150-8baee3776701</ns3:ID>
        <ns3:PaymentMeansCode listID="urn:tradeshift.com:api:1.0:paymentmeanscode">1</ns3:PaymentMeansCode>
        <ns3:PaymentDueDate>2013-09-18</ns3:PaymentDueDate>
    </ns5:PaymentMeans>
    <ns5:TaxTotal>
        <ns3:TaxAmount currencyID="USD">304.00</ns3:TaxAmount>
        <ns5:TaxSubtotal>
            <ns3:TaxableAmount currencyID="USD">4000.00</ns3:TaxableAmount>
            <ns3:TaxAmount currencyID="USD">304.00</ns3:TaxAmount>
            <ns5:TaxCategory>
                <ns3:ID schemeID="UN/ECE 5305" schemeAgencyID="6" schemeVersionID="D08B">S</ns3:ID>
                <ns3:Percent>7.6</ns3:Percent>
                <ns5:TaxScheme>
                    <ns3:ID schemeID="UN/ECE 5153 Subset" schemeAgencyID="6" schemeVersionID="D08B">STT</ns3:ID>
                    <ns3:Name>Tax</ns3:Name>
                </ns5:TaxScheme>
            </ns5:TaxCategory>
        </ns5:TaxSubtotal>
        <ns5:TaxSubtotal>
            <ns3:TaxableAmount currencyID="USD">2000.00</ns3:TaxableAmount>
            <ns3:TaxAmount currencyID="USD">0.00</ns3:TaxAmount>
            <ns5:TaxCategory>
                <ns3:ID schemeID="UN/ECE 5305" schemeAgencyID="6" schemeVersionID="D08B">E</ns3:ID>
                <ns3:Percent>0</ns3:Percent>
                <ns5:TaxScheme>
                    <ns3:ID schemeID="UN/ECE 5153 Subset" schemeAgencyID="6" schemeVersionID="D08B">TAX</ns3:ID>
                    <ns3:Name>Tax Exempt</ns3:Name>
                </ns5:TaxScheme>
            </ns5:TaxCategory>
        </ns5:TaxSubtotal>
    </ns5:TaxTotal>
    <ns5:LegalMonetaryTotal>
        <ns3:LineExtensionAmount currencyID="USD">6000.00</ns3:LineExtensionAmount>
        <ns3:TaxExclusiveAmount currencyID="USD">304.00</ns3:TaxExclusiveAmount>
        <ns3:TaxInclusiveAmount currencyID="USD">6304.00</ns3:TaxInclusiveAmount>
        <ns3:PayableAmount currencyID="USD">6304.00</ns3:PayableAmount>
    </ns5:LegalMonetaryTotal>
    <ns5:InvoiceLine>
        <ns3:ID>1</ns3:ID>
        <ns3:InvoicedQuantity unitCode="HUR">40</ns3:InvoicedQuantity>
        <ns3:LineExtensionAmount currencyID="USD">4000.00</ns3:LineExtensionAmount>
        <ns5:TaxTotal>
            <ns3:TaxAmount currencyID="USD">304.00</ns3:TaxAmount>
            <ns5:TaxSubtotal>
                <ns3:TaxableAmount currencyID="USD">4000.00</ns3:TaxableAmount>
                <ns3:TaxAmount currencyID="USD">304.00</ns3:TaxAmount>
                <ns5:TaxCategory>
                    <ns3:ID schemeID="UN/ECE 5305" schemeAgencyID="6" schemeVersionID="D08B">S</ns3:ID>
                    <ns3:Percent>7.6</ns3:Percent>
                    <ns5:TaxScheme>
                        <ns3:ID schemeID="UN/ECE 5153 Subset" schemeAgencyID="6" schemeVersionID="D08B">STT</ns3:ID>
                        <ns3:Name>Tax</ns3:Name>
                    </ns5:TaxScheme>
                </ns5:TaxCategory>
            </ns5:TaxSubtotal>
        </ns5:TaxTotal>
        <ns5:Item>
            <ns3:Description>Server Restore</ns3:Description>
            <ns3:Name>Server Restore</ns3:Name>
            <ns5:SellersItemIdentification>
                <ns3:ID schemeID="GTIN" schemeAgencyID="9">Server</ns3:ID>
            </ns5:SellersItemIdentification>
        </ns5:Item>
        <ns5:Price>
            <ns3:PriceAmount currencyID="USD">100.00</ns3:PriceAmount>
            <ns3:BaseQuantity unitCode="HUR">1</ns3:BaseQuantity>
            <ns3:OrderableUnitFactorRate>1</ns3:OrderableUnitFactorRate>
        </ns5:Price>
    </ns5:InvoiceLine>
    <ns5:InvoiceLine>
        <ns3:ID>2</ns3:ID>
        <ns3:InvoicedQuantity unitCode="HUR">20</ns3:InvoicedQuantity>
        <ns3:LineExtensionAmount currencyID="USD">2000.00</ns3:LineExtensionAmount>
        <ns5:TaxTotal>
            <ns3:TaxAmount currencyID="USD">0.00</ns3:TaxAmount>
            <ns5:TaxSubtotal>
                <ns3:TaxableAmount currencyID="USD">2000.00</ns3:TaxableAmount>
                <ns3:TaxAmount currencyID="USD">0.00</ns3:TaxAmount>
                <ns5:TaxCategory>
                    <ns3:ID schemeID="UN/ECE 5305" schemeAgencyID="6" schemeVersionID="D08B">E</ns3:ID>
                    <ns3:Percent>0</ns3:Percent>
                    <ns5:TaxScheme>
                        <ns3:ID schemeID="UN/ECE 5153 Subset" schemeAgencyID="6" schemeVersionID="D08B">TAX</ns3:ID>
                        <ns3:Name>Tax Exempt</ns3:Name>
                    </ns5:TaxScheme>
                </ns5:TaxCategory>
            </ns5:TaxSubtotal>
        </ns5:TaxTotal>
        <ns5:Item>
            <ns3:Description>Backup Processes</ns3:Description>
            <ns3:Name>Backup Processes</ns3:Name>
            <ns5:SellersItemIdentification>
                <ns3:ID schemeID="GTIN" schemeAgencyID="9">Backup</ns3:ID>
            </ns5:SellersItemIdentification>
        </ns5:Item>
        <ns5:Price>
            <ns3:PriceAmount currencyID="USD">100.00</ns3:PriceAmount>
            <ns3:BaseQuantity unitCode="HUR">1</ns3:BaseQuantity>
            <ns3:OrderableUnitFactorRate>1</ns3:OrderableUnitFactorRate>
        </ns5:Price>
    </ns5:InvoiceLine>
</ns9:Invoice>

--------------------------------

I came up with this XSLT:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://integration.cbre.com/schemas/gcs/invoice/v2"
    xmlns:common="http://integration.cbre.com/schemas/gcs/common/v1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="1.0" >

        <xsl:output method="xml" indent="yes"/>

        <xsl:template match="Invoice">
          <Invoice xmlns="http://integration.cbre.com/schemas/gcs/invoice/v2"
                 xmlns:common="http://integration.cbre.com/schemas/gcs/common/v1"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

            <ClientId>AMF-0001</ClientId>

            <OriginatingInvoiceId>43333</OriginatingInvoiceId>

            <VendorId>12345</VendorId>

            <AlternativeVendorId> </AlternativeVendorId>

            <VendorInvoiceNumber> </VendorInvoiceNumber>

            <InvoiceDate>1900-01-01T01:01:01-06:00</InvoiceDate>

            <InvoiceReceiveDate>1900-01-01T01:01:01-06:00</InvoiceReceiveDate>

            <GLDate>1900-01-01T01:01:01-06:00</GLDate>

            <Remark> 123333</Remark>

            <TotalInvoiceAmount>1</TotalInvoiceAmount>

            <CurrencyCode></CurrencyCode>

            <TaxRateAreaCode> </TaxRateAreaCode>

            <StatusCode>
              <common:Code>String</common:Code>
              <common:Description>String</common:Description>
            </StatusCode>

            <ClientApprovalNumber></ClientApprovalNumber>

            <AuthorizedBy></AuthorizedBy>

            <BatchReferenceNumber></BatchReferenceNumber>

            <InvoiceSource></InvoiceSource>

            <NumberOfLines>1</NumberOfLines>

            <InvoiceLineItems>

              <InvoiceLineItem>
                <OriginatingInvoiceLineID>1</OriginatingInvoiceLineID>
                <POLineNumber>1</POLineNumber>
                <LineItemDescription >test data</LineItemDescription >
                <LocationId > 1</LocationId>
                <BuildingName></BuildingName >
                <LineItemAmount>1</LineItemAmount>
                <TaxableLineItemAmount>1</TaxableLineItemAmount>
                <TaxAmountLineItem>1</TaxAmountLineItem>
                <GLAccountCode  ></GLAccountCode  >
                <AdditionalCoding></AdditionalCoding >
              </InvoiceLineItem>

              <InvoiceLineItem>
                <OriginatingInvoiceLineID>1</OriginatingInvoiceLineID>
                <POLineNumber>1</POLineNumber>
                <LineItemDescription >test data</LineItemDescription >
                <LocationId >4</LocationId>
                <BuildingName></BuildingName >
                <LineItemAmount>1</LineItemAmount>
                <TaxableLineItemAmount>1</TaxableLineItemAmount>
                <TaxAmountLineItem>1</TaxAmountLineItem>
                <GLAccountCode  >222</GLAccountCode  >
                <AdditionalCoding>3232</AdditionalCoding >
              </InvoiceLineItem>

            </InvoiceLineItems>

          </Invoice>
        </xsl:template>    

       
      </xsl:stylesheet>

--------------------------------------



I am confused with different namespaces on the xml file....

for e.g: if I want to map

From source
 <ns3:ID>AMF-0001</ns3:ID>

 to my output element <ClientId>

 i want to get started with an XSLT template...
LVL 8
mani_saiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
NEVER rely on the prefixes in the source document (unless they are obvious)
Bind all ns. namespaces to your own prefixes, that do make sense in your XSLT.
note that the ns1, ns2, ns3 etc namespaces are chosen randomly by the application that is given you the  XML. And they could change over time ns1 could become ns2 etc...
prefixes are not important, namespaces are. Prefixes are just a local reference to a namespace

have a
xmlns:ublc2="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
in the stylesheet element of your stylesheet

so you can use
<xsl:template match="ublc2:ID">
<ClientId><xsl:value-of select="."/></ClientId>
</xsl:template>

make sure the default namespace is not defined (xmlns="...", no prefix) or the output elements land in that namespace
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mani_saiAuthor Commented:
Thanks. i got the namespace fixed.

Here is XSLT:

<?xml version="1.0" encoding="utf-8"?>
     
      <xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:tdp="http://tradeshift.com/api/public/1.0"
          xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
          xmlns:cec="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
          xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
          xmlns:td="http://tradeshift.com/api/1.0"
          xmlns:appresp="urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2"
          xmlns:ordchg="urn:oasis:names:specification:ubl:schema:xsd:OrderChange-2"
          xmlns:inv="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
          xmlns:quo="urn:oasis:names:specification:ubl:schema:xsd:Quotation-2"
          xmlns:crdnte="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"
          xmlns:ord="urn:oasis:names:specification:ubl:schema:xsd:Order-2"
          xmlns:remind="urn:oasis:names:specification:ubl:schema:xsd:Reminder-2"
          xmlns:rmtadv="urn:oasis:names:specification:ubl:schema:xsd:RemittanceAdvice-2"
          xmlns:rcptadv="urn:oasis:names:specification:ubl:schema:xsd:ReceiptAdvice-2"
          xmlns:ordcan="urn:oasis:names:specification:ubl:schema:xsd:OrderCancellation-2"
          xmlns:reqforquot="urn:oasis:names:specification:ubl:schema:xsd:RequestForQuotation-2"
          xmlns:ordrespsim="urn:oasis:names:specification:ubl:schema:xsd:OrderResponseSimple-2"          
          xmlns:common="http://integration.cbre.com/schemas/gcs/common/v1"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="1.0" >

        <xsl:output method="xml" indent="yes"/>

        <xsl:template match="cbc:ID">
          <Invoice>

           
              <ClientId>
                <xsl:value-of select="cbc:ID"/>
              </ClientId>
           


           
          </Invoice>
        </xsl:template>    

       
      </xsl:stylesheet>

--------------------------------

i am getting lot of junk data in my output...

Say i just want to see the below output

        <Invoice>            
              <ClientId>1234  </ClientId>
         </Invoice>

-----------------------------------

i am trying to learn step by step...

Thanks
0
Geert BormansInformation ArchitectCommented:
It is important to know how the templating mechanism works.

When a node is "applied" to the templates, the template with the best match wins,
and its content will process the node

You only have one template, of a node somewhere deep down.

Still the processing starts at the document node of the source document.

In order to keep things going, there are built-in (hidden but always active at a low priority) templates
The built in template for elements does a simple apply-templates for the child nodes
(basically drop the tags but process the content)
The built in template for text nodes just outputs the text

That explains all the rubish you see

In order to get rid of the rubish you have two options

- option 1: override the text node template by not copying the text node to the output tree
for that add a template
<xsl:template match="text()"/>

- option 2: take control over the processing by being selective in what to process
for that add a template
<xsl:template match="/">
   <xsl:apply-templates select=".//cbc:ID"/>
</xsl:template>

both options have the same effect in this particular task

I hope it makes sense
0
mani_saiAuthor Commented:
Great. Thanks for the explanation.
0
mani_saiAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.