Applying an XSL Stylesheet

Now that you've created the stylesheet to transform an attribute-based XML file into an element-based file, you can use the transformNodeTo-Object method of the DOMDocument object to apply the stylesheet. The procedure that follows demonstrates how to do this programmatically.

1. Enter the code of the ApplyStyleSheet procedure, as shown below, in the same module where you've entered the code of the SaveRst_ADO procedure (XMLviaADO.xls).

2. Run the ApplyStyleSheet procedure to reformat the original XML file.

3. Open the C:\Products_Converted.xml file generated by the ApplyStyleSheet procedure (see Figure 17-28 following the procedure code).

Sub ApplyStyleSheet()

Dim myXMLDoc As New MSXML2.D0MDocument30 Dim myXSLDoc As New MSXML2.D0MDocument30 Dim newXMLDoc As New MSXML2.D0MDocument30 Dim strXML As String myXMLDoc.async = False If myXMLDoc.Load("C:\Products.xml") Then myXSLDoc.Load "C:\AttribToElem.xsl"

' apply the transformation

If Not myXSLDoc Is Nothing Then Set newXML = New D0MDocument30 myXMLDoc.transformNodeToObject myXSLDoc, newXMLDoc

' Save the output newXMLDoc.Save "C:\Products_Converted.xml" End If

End If

' Cleanup

Set myXMLDoc = Nothing Set myXSLDoc = Nothing Set newXMLDoc = Nothing

.; . >II- I I ' Pill ,1. p- : I HIP P' I P i: .« . P t '

-: Prod uctIO :■ 1 i/ProducilO > * prod uptNam p :~Ch a i ^/Prod uctNam e > ¿stipplierlD >i*/siipplierllJi < c 3teg ory [D i- Ï /taiegarvlD i-

<qidentityperurîit:- 1 a baltes H zo baqs--/QuantiPiFBiUmt> ■< u nit? ripe > 18 p .Un rtf n tx >

- : i r, 11 ? J n S t r, c k 3 9 -VU n rH ] n S hi ri:

End Sub

End Sub

.; . >II- I I ' Pill ,1. p- : I HIP P' I P i: .« . P t '

-: Prod uctIO :■ 1 i/ProducilO > * prod uptNam p :~Ch a i ^/Prod uctNam e > ¿stipplierlD >i*/siipplierllJi < c 3teg ory [D i- Ï /taiegarvlD i-

<qidentityperurîit:- 1 a baltes H zo baqs--/QuantiPiFBiUmt> ■< u nit? ripe > 18 p .Un rtf n tx >

- : i r, 11 ? J n S t r, c k 3 9 -VU n rH ] n S hi ri:

< tJ iiitiOnOrd Pr S-O-p/UliitsOftO t der pR eoppierLBvel 10 d 1RS ord prLe ve I p i C'ticontin upd > Pals e -p.'D iicontin up d > </Pn»dù et» --PrgHu^t;-

= Prùd D >2: /PrùduPHD > IP P III P N i' I ; 11 dl .Ml d / ' ' ' . d' ; > >

- d J.^ntiPiPr-l lnt-.?4 - [>7 b ntdl n *-p/p: i m ni r.1P-r nr

< u nisprice p-19-p AJriitpnce >

* i] nitsOnOrd Br > 4U p/un its 0 nOrde r> F! rnrdriLrvrl iRppld PrLp vr I :-

-p Pispgrvtin gcd p Fui* e^/D PûTïtin iiç d p--;/PPildupP;--i PPG dupP:-

0 0

Post a comment