Tải bản đầy đủ - 0 (trang)
Chapter 21. Working with XML in Excel

Chapter 21. Working with XML in Excel

Tải bản đầy đủ - 0trang

IntroductiontoExcel'sXMLFeatures

ThefirstthingtonoteabouttheXMLfeaturesdescribedinthis

chapteristhatmostofthemareavailableonlyinMicrosoft

OfficeProfessionalEdition2003andthestand-aloneMicrosoft

OfficeExcel2003.IfyouworkwithotherOfficeeditionssuchas

MicrosoftOfficeStandardEdition2003,MicrosoftOfficeStudent

andTeacherEdition2003,orMicrosoftOfficeBasicEdition

2003theXMLfeaturesdescribedinthischapterarenot

available.

ManyoftheXMLfeaturesofExcelareaccessedviaExcel'sXML

Sourcetaskpane.TobringuptheXMLSourcetaskpane,ifitis

notalreadydisplayed,chooseTaskPanefromExcel'sView

menu.Thetaskpanehasadrop-downlistfromwhichXML

Sourcecanbeselected,asshowninFigure21.1.



Figure21.1.SelectingXMLSourcefromthetask

panedrop-downlist.



AnalternativewayofaccessingtheXMLSourcetaskpaneisby

usingtheXMLsubmenuoftheDatamenu.TheXMLsubmenu

hasanXMLSourcecommandthatwillshowtheXMLSource

taskpane.Figure21.2showstheXMLsubmenu.Thischapter

examinesmanyofthecommandsintheXMLsubmenu.



Figure21.2.ChoosingData>XML>XMLSource.



[Viewfullsizeimage]



AfteryouhavemadetheXMLSourcetaskpanevisibleusing

oneofthesetwomethods,theXMLSourcetaskpanewill

appear,asshowninFigure21.3.



Figure21.3.TheXMLSourcetaskpane.



TheXMLSourcetaskpanereferstosomethingcalledanXML

map.AnXMLmapisamappingfromanXMLschematocells

and/orlistsintheworkbook.BeforeyoucreateanXMLmap,

youmusthaveanXMLschematoworkwith.Thefollowing

sectionexamineshowtocreateanXMLschemausingVisual

Studio2005.



IntroductiontoXMLSchemaCreationinVisual

Studio

VisualStudio2005hassupportforcreatingXMLschemas.

LaunchVisualStudio2005.ChooseFilefromtheNewmenu.

ThedialogboxshowninFigure21.4appears.PickXMLSchema

fromthisdialogboxandthenclicktheOpenbutton.



Figure21.4.CreatinganewXMLschemafile.



[Viewfullsizeimage]



VisualStudioshowsadesignviewforcreatingXMLschema,as

showninFigure21.5.ThetoolboxhasXMLschemaobjectsthat

canbedraggedontothedesignsurfaceforthenewXML

schema.



Figure21.5.Designview,creatinganewXML



schemafile.



[Viewfullsizeimage]



Theschemaobjectwewillusemostfrequentlyinthisexample

iselement.AnXMLschemaelementdefinesanelementinan

XMLfile.ThesimpleXMLfileshowninListing21.1,for

example,hasanelementcalledOrderinthenamespacens1.It

alsohasanelementcalledCustomerNameinthenamespace

ns1.TheCustomerNameelementisparentedbytheOrder

element.



Listing21.1.XMLFileRepresentingaSimple

Order







EricCarter





TheXMLschemaforthissimpleOrderXMLfileiscreatedby

followingthesesteps:

1. Draganelementfromthetoolboxontotheschemadesign

surface.

2. Intheheaderrowofthenewlycreatedelementnexttothe

E,typeOrder.

3. Inthe*rownexttotheasterisk(*),typeCustomerName.



Figure21.6showstheresultingdesignerview.



Figure21.6.DesignviewofasimpleOrder

schema.



Whenyousavethisschema,usetheSaveAscommandfrom

theFilemenutosaveitasorder.xsd.YouwillhavetopickXML

SchemaFiles(*.xsd)fromtheSaveAsTypedrop-downlistin



theSaveAsdialogbox.Theorder.xsdfilewilllookliketheone

showninListing21.2.YoucanseethatanXMLschemaisjust

anotherXMLfilethatdefineswhatconstitutesavalidOrderXML

file.Itdefinestwoelements:OrderandCustomerName.

BecausetheOrderelementcontainsotherelements,itis

definedasacomplexType.Itcontainsasequenceof

CustomerNameelementsthatareoftypestring.Sequencein

thiscaseismisleading;thewaytheXSDfileisdefineditwillbe

asequenceofone,andonlyone,CustomerNameelement.Itis

possibletodefineasequencethathasavaryingnumberof

elementsinitusingthemaxOccursandminOccurssettings,

whichweconsiderlaterinthischapter.BysettingminOccursto

1andmaxOccurstounbounded,forexample,youcouldallowone

ormoreCustomerNameelementstobeassociatedwithan

order.



Listing21.2.XSDSchemaFileforaSimpleOrder

Schema






elementFormDefault="qualified"

xmlns="http://tempuri.org/XMLSchema.xsd"xmlns:mstns="http://tempuri.org/

XMLSchema.xsd"

xmlns:xs="http://www.w3.org/2001/XMLSchema">








type="xs:string"/>











Notethatthisschemaisdefinedentirelywithelements.An

alternativewayofrepresentingthissamedataistousean

OrderelementandaCustomerNameattribute.If



CustomerNameisdefinedasanattribute,theresultantXMLis

asshowninListing21.3.



Listing21.3.XMLFileforaSimpleOrderThat

UsesanAttribute






CustomerName="EricCarter">





TheXMLschemaforanOrderXMLfilethatusesanattributeis

createdinVisualStudiobyfollowingthesesteps:

1. Draganelementfromthetoolboxontotheschemadesign

surface.

2. Intheheaderrowofthenewlycreatedelementnexttothe

E,typeOrder.

3. Inthe*rownexttotheasterisk(*),typeCustomerName.

4. ClicktheEnexttoCustomerName.

Adrop-downlistwillappear.

5. Selectanattributefromthedrop-downlisttoconvert

CustomerNametoanattribute.



Figure21.7showstheresultantdesignerview.



Figure21.7.DesignviewofasimpleOrder



schemathatusesanattribute.



Listing21.4showstheschemaforanorderusinganattribute.

BecausetheOrderelementcontainsotherattributes,itis

definedasacomplexType.Itcontainsanemptysequence;this

sequencecanactuallyberemovedwithoutaffectingthe

schema.ThenitdefinesCustomerNameasanattributeoftype

string.



Listing21.4.XSDSchemaFileforaSimpleOrder

SchemaThatUsesanAttribute






elementFormDefault="qualified"

xmlns="http://tempuri.org/XMLSchema.xsd"

xmlns:mstns="http://tempuri.org/XMLSchema.xsd"

xmlns:xs="http://www.w3.org/2001/XMLSchema">

















Excelworksequallywellwithschemasthatuseattributesor



elements.Word,however,doesnotworkverywellwhenyou

useattributesinaschema.Ifyouarecreatingaschemathat

youneedtouseinExcelandWord,youshouldtrytouse

elementsinsteadofattributes.Formoreinformation,see

Chapter22,"WorkingwithXMLinWord."



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 21. Working with XML in Excel

Tải bản đầy đủ ngay(0 tr)

×
x