Avatar billede mungojerrie Nybegynder
12. april 2005 - 10:48 Der er 17 kommentarer og
3 løsninger

Kopiering af noder fra eksisterende xml dokument til nyt dokument

Hej

Jeg har fået lavet mig en "generel" xml dokument som jeg gerne vil tage informationer ud fra til en kopi, men kopien skal ikke indeholde det hele.....
hvordan gør jeg det smartest ?

Udgangspunkt:

<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/" xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/" xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/ http://rep.oio.dk/ubl/xml/schemas/0p71/pie/pielax.xsd">
  <com:ID>2442012</com:ID>
  <com:IssueDate>2005-03-29</com:IssueDate>
  <com:TypeCode>PIE</com:TypeCode>
  <main:InvoiceCurrencyCode>DKK</main:InvoiceCurrencyCode>
  <com:Note>HDR</com:Note>
  <com:BuyersReferenceID schemeID="EAN">5798002230314</com:BuyersReferenceID>
  <com:ReferencedOrder>
    <com:BuyersOrderID>Linjespecificeret</com:BuyersOrderID>
    <com:SellersOrderID>null</com:SellersOrderID>
    <com:IssueDate>2005-03-15</com:IssueDate>
  </com:ReferencedOrder>
  .
  .
  .
  .
  .
  .
  .
</Invoice>

Resultat:

<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/" xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/" xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/ http://rep.oio.dk/ubl/xml/schemas/0p71/pie/pielax.xsd">
  <com:ID>2442012</com:ID>
  <com:IssueDate>2005-03-29</com:IssueDate>
  <com:TypeCode>PIE</com:TypeCode>
  <main:InvoiceCurrencyCode>DKK</main:InvoiceCurrencyCode>
  <com:BuyersReferenceID schemeID="EAN">5798002230314</com:BuyersReferenceID>
  <com:ReferencedOrder>
    <com:BuyersOrderID>Linjespecificeret</com:BuyersOrderID>
    <com:SellersOrderID>null</com:SellersOrderID>
    <com:IssueDate>2005-03-15</com:IssueDate>
  </com:ReferencedOrder>
</Invoice>
Avatar billede Slettet bruger
12. april 2005 - 12:11 #1
Hvis det eneste du skal have ud er rod-elementet (<Invoice>) + <com:ReferencedOrder>, så brug denne template:

<xsl:template match="/">
  <xsl:copy>
      <xsl:copy-of select="com:ReferencedOrder" />
  </xsl:copy>
</xsl:template>

/CS
Avatar billede mungojerrie Nybegynder
12. april 2005 - 14:54 #2
hmm, det synes jeg ellers jeg har prøvet uden det store held, men det kan jo være jeg har lavet en fejl et sted....

XSL:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/"
xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/"
xmlns:pie="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/"
>
<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
        <xsl:copy-of select="//com:TypeCode"/>
</xsl:template>
</xsl:stylesheet>

XML udgangspunkt:

<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/" xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/" xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/" xmlns:ext="http://www.evikali.dk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/ http://rep.oio.dk/ubl/xml/schemas/0p71/pie/pielax.xsd">
  <com:ID>2442012</com:ID>
  <com:IssueDate>2005-03-29</com:IssueDate>
  <com:TypeCode>PIE</com:TypeCode>
  <main:InvoiceCurrencyCode>DKK</main:InvoiceCurrencyCode>
  <com:Note>HDR</com:Note>
  <com:BuyersReferenceID schemeID="EAN">5798002230314</com:BuyersReferenceID>
  <com:ReferencedOrder>
</Invoice>

XML resultat:
<com:TypeCode xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/" xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/" xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/" xmlns:ext="http://www.evikali.dk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">PIE</com:TypeCode><com:TypeCode xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/" xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/" xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/" xmlns:ext="http://www.evikali.dk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">null</com:TypeCode><com:TypeCode xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/" xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/" xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/" xmlns:ext="http://www.evikali.dk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">VAT</com:TypeCode><com:TypeCode xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/" xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/" xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/" xmlns:ext="http://www.evikali.dk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">VAT</com:TypeCode>
Avatar billede mungojerrie Nybegynder
12. april 2005 - 14:55 #3
årsagen til at com:TypeCode's værdi er VAT, skyldes at jeg ikke har vist hele mit udgangspunkt, da den er meget lang og den indeholder altså en TypeCode node længere nede som desværre bliver valgt i stedet for den i toppen.....
Avatar billede Slettet bruger
12. april 2005 - 15:26 #4
OK - men hvad er det helt nøjagtigt du skal bruge? Er det f.eks. kun den første <ReferencedOrder> eller er det kun <TypeCode> fra en bestemt <ReferencedOrder> eller er det noget helt tredje... ?

/CS
Avatar billede Slettet bruger
12. april 2005 - 15:28 #5
PS: Din TypeCode "VAT" bliver ikke valgt "i stedet for" - den bliver valgt sammen med, da "//com:TypeCode" matcher samtlige TypeCode elementer i "com" prefixets namespace

/CS
Avatar billede mungojerrie Nybegynder
12. april 2005 - 15:40 #6
jeg skal skabe et xml dokument magen til den jeg starter ud med, men der er bare en del noder som ikke skal være med i slutresultatet.....
Dokumentet er lidt for langt til at poste her tror jeg, så hvis jeg bare kan få startet på at spyttet de første noder ud, så kan jeg jo nok selv klare resten.....
Gav det mening ?
Avatar billede Slettet bruger
12. april 2005 - 16:18 #7
OK - start med en template der kopierer hele source dokumentet:

<xsl:template match="/ | @* | node()">
  <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

og tilføj tomme templates for de elementer du IKKE vil have med, f.eks. alle <ReferencedOrder> hvor <TypeCode> er "VAT" :

<xsl:template match="com:ReferencedOrder[com:TypeCode = 'VAT']" />

- und so weiter...

/CS
Avatar billede mungojerrie Nybegynder
13. april 2005 - 09:06 #8
Hmm, det duer ikke helt som jeg forventede....jeg får ganske vist alt der er indeni min Invoice tag, men jeg skal jo også gerne have
<?xml version="1.0" encoding="utf-8"?>
i toppen af dokumentet.....
Avatar billede mungojerrie Nybegynder
13. april 2005 - 09:17 #9
kan jeg ikke også angive at outputtet skal formateres pænt, så det hele ikke bare kommer i een lang streng ? det gør det jo noget nemmere at tjekke for evt fejl.....
Avatar billede mungojerrie Nybegynder
13. april 2005 - 09:19 #10
bare drop min sidste bemærkning.....
Avatar billede Slettet bruger
13. april 2005 - 09:29 #11
Du klarer dem begge vha. <xsl:output> elementet:

<xsl:stylesheet [bla bla bla...]>
  <xsl:output method="xml" indent="yes" />
  ...
</xsl:stylesheet>
Avatar billede mungojerrie Nybegynder
13. april 2005 - 09:33 #12
Okay, synes jo bare jeg har den output tag med:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/"
xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/"
xmlns:pie="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/"
>
<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match="/ | @* | node()">
  <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="com:Note" />
<xsl:template match="com:SellersOrderID" />
<xsl:template match="//com:BuyerParty/com:AccountCode" />
<xsl:template match="//com:BuyerParty/com:Address/com:Country" />
<xsl:template match="//com:Address/com:ExtensibleContent" />
<xsl:template match="com:PayeeFinancialAccount" />

</xsl:stylesheet>
Avatar billede Slettet bruger
13. april 2005 - 12:58 #13
Ja det synes jeg da også du har :-)

Du kan specifikt bede den om at outputte XML deklarationen vha. "omit-xml-declaration" attributten, men den skal være "no" som default. Bruger du MSXML eller en anden parser?

Men altså:
<xsl:output
  method="xml"
  encoding="utf-8"
  indent="yes"
  omit-xml-declaration="no"
/>

("indent" tillader bare ekstra whitespace i outputtet - den kræver ikke at det formatteres "pænt")

/CS
Avatar billede Slettet bruger
13. april 2005 - 12:59 #14
PS: "//" foran et element i en <template>'s match attribut er unødvendig (underforstået).

/CS
Avatar billede mungojerrie Nybegynder
13. april 2005 - 16:44 #15
1. Jeg bruger .net (C#) til at generere mit output
2. mht // i match delen : hvis jeg undlader disse, hvordan kan jeg så sikre mig at jeg rammer den rigtige del af dokumentet ?
Jeg har nemlig flere af samme node forskellige steder i dokumentet og det er ikke alle steder noden skal fjernes.
ex:
<Invoice>
<com:AllowanceCharge>
.....
</com:AllowanceCharge>
<InvoiceLine>
<com:AllowanceCharge>
.....
</com:AllowanceCharge>
<com:AllowanceCharge>
.....
</com:AllowanceCharge>
</InvoiceLine>
</Invoice>

AllowanceCharge skal ikke fjernes fra InvoiceLine delen......
Avatar billede Slettet bruger
14. april 2005 - 01:36 #16
2. >> Der må *ikke* være forskel i outputtet fra en match på f.eks. "//element" og "element" - ellers er parseren ubrugelig.

Fidusen er netop den du har brugt i et par stykker allerede: Man fortæller vha. match-attributten at template'en skal bruges på <AllowanceCharge> elementer indeni et <InvoiceLine> element. Eller i dit tilfælde: Brug denne template på <AllowanceCharge> elementer der ikke ligger i et <InvoiceLine> element:

<xsl:template match="com:AllowanceCharge[not(parent::com:InvoiceLine)]" />

/CS
Avatar billede mungojerrie Nybegynder
14. april 2005 - 09:20 #17
super, det virker jo bare, men jeg kan altså stadig ikke få min xml declaration med i outputet.....

min transform.xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:main="http://rep.oio.dk/ubl/xml/schemas/0p71/maindoc/"
xmlns:com="http://rep.oio.dk/ubl/xml/schemas/0p71/common/"
xmlns:pie="http://rep.oio.dk/ubl/xml/schemas/0p71/pie/"
>
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>

<xsl:template match="/ | @* | node()">
  <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="com:Note" />

<xsl:template match="com:SellersOrderID" />

<xsl:template match="com:BuyerParty/com:AccountCode" />
<xsl:template match="com:BuyerParty/com:Address/com:Country" />
<xsl:template match="com:BuyerParty/com:BuyerContact/com:Name" />
<xsl:template match="com:BuyerParty/com:BuyerContact/com:Phone" />
<xsl:template match="com:BuyerParty/com:BuyerContact/com:Fax" />
<xsl:template match="com:BuyerParty/com:BuyerContact/com:E-Mail" />
<xsl:template match="com:BuyerParty/com:BuyerContact/com:Role" />
<xsl:template match="com:BuyerParty/com:ExtensibleContent" />

<xsl:template match="com:DestinationParty" />

<xsl:template match="com:SellerParty/com:BuyersID" />
<xsl:template match="com:SellerParty/com:Address/com:Country" />
<xsl:template match="com:SellerParty/com:OrderContact" />
<xsl:template match="com:SellerParty/com:ExtensibleContent" />

<xsl:template match="com:PaymentMeans/com:PayeeFinancialAccount" />
<xsl:template match="com:PaymentMeans/com:PaymentAdvice" />
<xsl:template match="com:PaymentMeans/com:ExtensibleContent" />

<xsl:template match="com:AllowanceCharge[not(parent::com:InvoiceLine)]" />

<xsl:template match="com:InvoiceLine/com:Note" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:DestinationParty" />

<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:Description" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:BuyersItemIdentification" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:SellersItemIdentification" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:ManufacturersItemIdentification" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:StandardItemIdentification" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:CatalogueItemIdentification" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:ReferencedCatalogue" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:CommodityClassification" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:Tax" />
<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:Item/com:BasePrice" />

<xsl:template match="com:InvoiceLine/com:ReferencedOrderLine/com:DeliveryRequirement/com:DeliverySchedule" />

<xsl:template match="com:InvoiceLine/com:Item/com:StandardItemIdentification" />
<xsl:template match="com:InvoiceLine/com:Item/com:CatalogueItemIdentification" />
<xsl:template match="com:InvoiceLine/com:Item/com:ReferencedCatalogue" />
<xsl:template match="com:InvoiceLine/com:Item/com:CommodityClassification" />
<xsl:template match="com:InvoiceLine/com:Item/com:Tax" />
<xsl:template match="com:InvoiceLine/com:Item/com:BasePrice" />

<xsl:template match="com:InvoiceLine/com:AllowanceCharge/com:MultiplierReasonCode" />
<xsl:template match="com:InvoiceLine/com:AllowanceCharge/com:MultiplierFactorQuantity" />
<xsl:template match="com:InvoiceLine/com:AllowanceCharge/com:BuyersReferenceID" />

</xsl:stylesheet>
Avatar billede mungojerrie Nybegynder
14. april 2005 - 09:33 #18
jeg ved ikke om det er det rigtige sted at fikse min xml declaration problem, men jeg har fået min c# kode til at skrive den ud, så du får en accept af dit svar og mange tak for hjælpen :-)
Avatar billede Slettet bruger
14. april 2005 - 12:34 #19
hehe :-)

Med *så* mange undtagelser kunne det jo godt være det var nemmere at fortælle hvad du *gerne* vil have ud, istedet for det du iike vili have ud - men du siger jo det er en MEGA fil, så.... :-)

/CS
Avatar billede mungojerrie Nybegynder
14. april 2005 - 13:46 #20
tja, det kan jo godt være en anden fremgangsmåde er smartere og du må da gerne smide en kommentar med hvordan jeg evt skulle gøre det så. Der kan jo også være noget performance i det, da jeg skal konvertere mange filer (500-1500). Mit program er dog ikke mission critical og skal ikke klare opgaven indenfor en bestemt tidsramme.....
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester