首页/技术开发/内容

最新XML介绍系列(6)

技术开发2024-01-17 阅读()
系列之十二:格式化XML(一)

使用JavaScript格式化XML文档
你如果要想把XML文档中的数据显示在HTML页面中,
你可以使用JavaScript来导入数据,
你要想看到XML和HTML之间是如何互补的话,可以
比较这个XML文档(note.xml)和这个HTML文档(note.htm使用了JavaScript
从XML中读出数据然后显示在HTML中)
文件代码如下:
note.xml
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

note.htm
<html>
<head>

<script language="JavaScript" for="window" event="onload">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("note.xml");
nodes = xmlDoc.documentElement.childNodes;
to.innerText = nodes.item(0).text;
from.innerText = nodes.item(1).text;
header.innerText = nodes.item(2).text;
body.innerText = nodes.item(3).text;
</script>

<title>HTML using XML data</title>
</head>

<body bgcolor="yellow">

<h1>Refsnes Data Internal Note</h1>

<b>To: </b>
<span id="to"> </span>
<br>

<b>From: </b>
<span id="from"></span>
<hr>

<b><span id="header"></span></b>

<hr>
<span id="body"></span>

</body>
</html>

翻译:Batman
系列之十三:格式化XML(二)
使用CSS格式化XML
下面来示范如何使用CSS文件来格式化XML文件
shakespeare.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="shakespeare.css"?>
<shakespeare-index>
<play>
<TITLE>All's Well That Ends Well</TITLE>
<path>all_well.xml</path>
<PERSONAE>
<TITLE>Dramatis Personae</TITLE>
<PERSONA>KING OF FRANCE</PERSONA>
<PERSONA>DUKE OF FLORENCE</PERSONA>
<PERSONA>BERTRAM, Count of Rousillon.</PERSONA>
<PERSONA>LAFEU, an old lord.</PERSONA>
<PERSONA>PAROLLES, a follower of Bertram.</PERSONA>
<PGROUP>
<PERSONA>Steward</PERSONA>
<PERSONA>Clown</PERSONA>
<GRPDESCR>servants to the Countess of Rousillon.</GRPDESCR>
</PGROUP>
<PERSONA>A Page. </PERSONA>
<PERSONA>COUNTESS OF ROUSILLON, mother to Bertram. </PERSONA>
<PERSONA>HELENA, a gentlewoman protected by the Countess.</PERSONA>
<PERSONA>An old Widow of Florence. </PERSONA>
<PERSONA>DIANA, daughter to the Widow.</PERSONA>
<PGROUP>
<PERSONA>VIOLENTA</PERSONA>
<PERSONA>MARIANA</PERSONA>
<GRPDESCR>neighbours and friends to the Widow.</GRPDESCR>
</PGROUP>
<PERSONA>Lords, Officers, Soldiers, &amp;c., French and Florentine.</PERSONA>
</PERSONAE>
</play>

<play>
<TITLE>As You Like It</TITLE>
<path>as_you.xml</path>
<PERSONAE>
<TITLE>Dramatis Personae</TITLE>

<PERSONA>DUKE SENIOR, living in banishment.</PERSONA>
<PERSONA>DUKE FREDERICK, his brother, an usurper of his dominions.</PERSONA>

<PGROUP>
<PERSONA>AMIENS</PERSONA>
<PERSONA>JAQUES</PERSONA>
<GRPDESCR>lords attending on the banished duke.</GRPDESCR>
</PGROUP>

<PERSONA>LE BEAU, a courtier attending upon Frederick.</PERSONA>
<PERSONA>CHARLES, wrestler to Frederick.</PERSONA>

<PGROUP>
<PERSONA>OLIVER</PERSONA>
<PERSONA>JAQUES </PERSONA>
<PERSONA>ORLANDO</PERSONA>
<GRPDESCR>sons of Sir Rowland de Boys.</GRPDESCR>
</PGROUP>


<PGROUP>
<PERSONA>ADAM</PERSONA>
<PERSONA>DENNIS</PERSONA>
<GRPDESCR>servants to Oliver.</GRPDESCR>
</PGROUP>

<PERSONA>TOUCHSTONE, a clown.</PERSONA>
<PERSONA>SIR OLIVER MARTEXT, a vicar.</PERSONA>

<PGROUP>
<PERSONA>CORIN</PERSONA>
<PERSONA>SILVIUS</PERSONA>
<GRPDESCR>shepherds.</GRPDESCR>
</PGROUP>

<PERSONA>WILLIAM, a country fellow in love with Audrey.</PERSONA>
<PERSONA>A person representing HYMEN. </PERSONA>
<PERSONA>ROSALIND, daughter to the banished duke.</PERSONA>
<PERSONA>CELIA, daughter to Frederick.</PERSONA>
<PERSONA>PHEBE, a shepherdess.</PERSONA>
<PERSONA>AUDREY, a country wench.</PERSONA>
<PERSONA>Lords, pages, and attendants, &amp;c.</PERSONA>
</PERSONAE>

</play>

<play>
<TITLE>A Midsummer Night's Dream</TITLE>
<path>dream.xml</path>
<PERSONAE>
<TITLE>Dramatis Personae</TITLE>

<PERSONA>THESEUS, Duke of Athens.</PERSONA>
<PERSONA>EGEUS, father to Hermia.</PERSONA>

<PGROUP>
<PERSONA>LYSANDER</PERSONA>
<PERSONA>DEMETRIUS</PERSONA>
<GRPDESCR>in love with Hermia.</GRPDESCR>
</PGROUP>

<PERSONA>PHILOSTRATE, master of the revels to Theseus.</PERSONA>
<PERSONA>QUINCE, a carpenter.</PERSONA>
<PERSONA>SNUG, a joiner.</PERSONA>
<PERSONA>BOTTOM, a weaver.</PERSONA>
<PERSONA>FLUTE, a bellows-mender.</PERSONA>
<PERSONA>SNOUT, a tinker.</PERSONA>
<PERSONA>STARVELING, a tailor.</PERSONA>
<PERSONA>HIPPOLYTA, queen of the Amazons, betrothed to Theseus.</PERSONA>
<PERSONA>HERMIA, daughter to Egeus, in love with Lysander.</PERSONA>
<PERSONA>HELENA, in love with Demetrius.</PERSONA>
<PERSONA>OBERON, king of the fairies.</PERSONA>
<PERSONA>TITANIA, queen of the fairies.</PERSONA>
<PERSONA>PUCK, or Robin Goodfellow.</PERSONA>

<PGROUP>
<PERSONA>PEASEBLOSSOM</PERSONA>
<PERSONA>COBWEB</PERSONA>
<PERSONA>MOTH</PERSONA>
<PERSONA>MUSTARDSEED</PERSONA>
<GRPDESCR>fairies.</GRPDESCR>
</PGROUP>

<PERSONA>Other fairies attending their King and Queen.</PERSONA>
<PERSONA>Attendants on Theseus and Hippolyta.</PERSONA>
</PERSONAE>
</play>

<play>
<TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>
<path>hamlet.xml</path>
<PERSONAE>
<TITLE>Dramatis Personae</TITLE>

<PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
<PERSONA>HAMLET, son to the late, and nephew to the present king.</PERSONA>
<PERSONA>POLONIUS, lord chamberlain. </PERSONA>
<PERSONA>HORATIO, friend to Hamlet.</PERSONA>
<PERSONA>LAERTES, son to Polonius.</PERSONA>
<PERSONA>LUCIANUS, nephew to the king.</PERSONA>

<PGROUP>
<PERSONA>VOLTIMAND</PERSONA>
<PERSONA>CORNELIUS</PERSONA>
<PERSONA>ROSENCRANTZ</PERSONA>
<PERSONA>GUILDENSTERN</PERSONA>
<PERSONA>OSRIC</PERSONA>
<GRPDESCR>courtiers.</GRPDESCR>
</PGROUP>

<PERSONA>A Gentleman</PERSONA>
<PERSONA>A Priest. </PERSONA>

<PGROUP>
<PERSONA>MARCELLUS</PERSONA>
<PERSONA>BERNARDO</PERSONA>
<GRPDESCR>officers.</GRPDESCR>
</PGROUP>

<PERSONA>FRANCISCO, a soldier.</PERSONA>
<PERSONA>REYNALDO, servant to Polonius.</PERSONA>
<PERSONA>Players.</PERSONA>
<PERSONA>Two Clowns, grave-diggers.</PERSONA>
<PERSONA>FORTINBRAS, prince of Norway. </PERSONA>
<PERSONA>A Captain.</PERSONA>
<PERSONA>English Ambassadors. </PERSONA>
<PERSONA>GERTRUDE, queen of Denmark, and mother to Hamlet. </PERSONA>
<PERSONA>OPHELIA, daughter to Polonius.</PERSONA>
<PERSONA>Lords, Ladies, Officers, Soldiers, Sailors, Messengers, and other Attendants.</PERSONA>
<PERSONA>Ghost of Hamlet's Father. </PERSONA>
</PERSONAE>

</play>

<play>
<TITLE>The Life of Henry the Fifth</TITLE>
<path>hen_v.xml</path>
<PERSONAE>
<TITLE>Dramatis Personae</TITLE>

<PERSONA>KING HENRY, the Fifth. </PERSONA>

<PGROUP>
<PERSONA>DUKE OF GLOUCESTER</PERSONA>
<PERSONA>DUKE OF BEDFORD</PERSONA>
<GRPDESCR>brothers to the King.</GRPDESCR>
</PGROUP>

<PERSONA>DUKE OF EXETER, uncle to the King. </PERSONA>
<PERSONA>DUKE OF YORK, cousin to the King. </PERSONA>
<PERSONA>EARL OF SALISBURY</PERSONA>
<PERSONA>EARL OF WESTMORELAND</PERSONA>
<PERSONA>EARL OF WARWICK</PERSONA>
<PERSONA>BISHOP OF CANTERBURY</PERSONA>
<PERSONA>BISHOP OF ELY</PERSONA>
<PERSONA>EARL OF CAMBRIDGE</PERSONA>
<PERSONA>LORD SCROOP</PERSONA>
<PERSONA>SIR THOMAS GREY</PERSONA>

<PGROUP>
<PERSONA>SIR THOMAS ERPINGHAM</PERSONA>
<PERSONA>GOWER</PERSONA>
<PERSONA>FLUELLEN</PERSONA>
<PERSONA>MACMORRIS</PERSONA>
<PERSONA>JAMY</PERSONA>
<GRPDESCR>Officers in King Henry's army.</GRPDESCR>
</PGROUP>


<PGROUP>
<PERSONA>BATES</PERSONA>
<PERSONA>COURT</PERSONA>
<PERSONA>WILLIAMS</PERSONA>
<GRPDESCR>soldiers in the same.</GRPDESCR>
</PGROUP>

<PERSONA>PISTOL</PERSONA>
<PERSONA>NYM</PERSONA>
<PERSONA>BARDOLPH</PERSONA>
<PERSONA>Boy</PERSONA>
<PERSONA>A Herald.</PERSONA>
<PERSONA>CHARLES the Sixth, King of France. </PERSONA>
<PERSONA>LEWIS, the Dauphin. </PERSONA>
<PERSONA>DUKE OF BURGUNDY</PERSONA>
<PERSONA>DUKE OF ORLEANS</PERSONA>
<PERSONA>DUKE OF BOURBON</PERSONA>
<PERSONA>The Constable of France. </PERSONA>

<PGROUP>
<PERSONA>RAMBURES</PERSONA>
<PERSONA>GRANDPRE</PERSONA>
<GRPDESCR>French Lords.</GRPDESCR>
</PGROUP>

<PERSONA>GOVERNOR, of Harfleur.</PERSONA>
<PERSONA>MONTJOY, a French Herald.</PERSONA>
<PERSONA>Ambassadors to the King of England.</PERSONA>
<PERSONA>ISABEL, Queen of France. </PERSONA>
<PERSONA>KATHARINE, daughter to Charles and Isabel.</PERSONA>
<PERSONA>ALICE, a lady attending on her.</PERSONA>
<PERSONA>Hostess of a tavern in Eastcheap formerly Mistress Quickly, and now married to Pistol.</PERSONA>
<PERSONA>Lords, Ladies, Officers, Soldiers, Citizens, Messengers, and Attendants. Chorus.</PERSONA>
</PERSONAE>

</play>

<play>
<TITLE>The Tragedy of King Lear</TITLE>
<path>lear.xml</path>

<PERSONAE>
<TITLE>Dramatis Personae</TITLE>

<PERSONA>LEAR, king of Britain </PERSONA>
<PERSONA>KING OF FRANCE</PERSONA>
<PERSONA>DUKE OF BURGUNDY</PERSONA>
<PERSONA>DUKE OF CORNWALL</PERSONA>
<PERSONA>DUKE OF ALBANY</PERSONA>
<PERSONA>EARL OF KENT</PERSONA>
<PERSONA>EARL OF GLOUCESTER</PERSONA>
<PERSONA>EDGAR, son to Gloucester.</PERSONA>
<PERSONA>EDMUND, bastard son to Gloucester.</PERSONA>
<PERSONA>CURAN, a courtier.</PERSONA>
<PERSONA>Old Man, tenant to Gloucester.</PERSONA>
<PERSONA>Doctor</PERSONA>
<PERSONA>Fool</PERSONA>
<PERSONA>OSWALD, steward to Goneril.</PERSONA>
<PERSONA>A Captain employed by Edmund. </PERSONA>
<PERSONA>Gentleman attendant on Cordelia. </PERSONA>
<PERSONA>A Herald.</PERSONA>
<PERSONA>Servants to Cornwall.</PERSONA>

<PGROUP>
<PERSONA>GONERIL</PERSONA>
<PERSONA>REGAN</PERSONA>
<PERSONA>CORDELIA</PERSONA>
<GRPDESCR>daughters to Lear.</GRPDESCR>
</PGROUP>

<PERSONA>Knights of Lear's train, Captains, Messengers, Soldiers, and Attendants</PERSONA>
</PERSONAE>

</play>

<play>
<TITLE>The Tragedy of Macbeth</TITLE>
<path>macbeth.xml</path>

<PERSONAE>
<TITLE>Dramatis Personae</TITLE>

<PERSONA>DUNCAN, king of Scotland.</PERSONA>

<PGROUP>
<PERSONA>MALCOLM</PERSONA>
<PERSONA>DONALBAIN</PERSONA>
<GRPDESCR>his sons.</GRPDESCR>
</PGROUP>


<PGROUP>
<PERSONA>MACBETH</PERSONA>
<PERSONA>BANQUO</PERSONA>
<GRPDESCR>generals of the king's army.</GRPDESCR>
</PGROUP>


<PGROUP>
<PERSONA>MACDUFF</PERSONA>
<PERSONA>LENNOX</PERSONA>
<PERSONA>ROSS</PERSONA>
<PERSONA>MENTEITH</PERSONA>
<PERSONA>ANGUS</PERSONA>
<PERSONA>CAITHNESS</PERSONA>
<GRPDESCR>noblemen of Scotland.</GRPDESCR>
</PGROUP>

<PERSONA>FLEANCE, son to Banquo.</PERSONA>
<PERSONA>SIWARD, Earl of Northumberland, general of the English forces.</PERSONA>
<PERSONA>YOUNG SIWARD, his son.</PERSONA>
<PERSONA>SEYTON, an officer attending on Macbeth.</PERSONA>
<PERSONA>Boy, son to Macduff. </PERSONA>
<PERSONA>An English Doctor. </PERSONA>
<PERSONA>A Scotch Doctor. </PERSONA>
<PERSONA>A Soldier.</PERSONA>
<PERSONA>A Porter.</PERSONA>
<PERSONA>An Old Man.</PERSONA>
<PERSONA>LADY MACBETH</PERSONA>
<PERSONA>LADY MACDUFF</PERSONA>
<PERSONA>Gentlewoman attending on Lady Macbeth. </PERSONA>
<PERSONA>HECATE</PERSONA>
<PERSONA>Three Witches.</PERSONA>
<PERSONA>Apparitions.</PERSONA>
<PERSONA>Lords, Gentlemen, Officers, Soldiers, Murderers, Attendants, and Messengers. </PERSONA>
</PERSONAE>

</play>

<play>
<TITLE>The Tragedy of Richard the Third</TITLE>
<path>rich_iii.xml</path>

<PERSONAE>
<TITLE>Dramatis Personae</TITLE>

<PERSONA>KING EDWARD The Fourth</PERSONA>

<PGROUP>
<PERSONA>EDWARD, Prince of Wales, afterwards King Edward V.</PERSONA>
<PERSONA>RICHARD, Duke of York </PERSONA>
<GRPDESCR>sons to the King.</GRPDESCR>
</PGROUP>


<PGROUP>
<PERSONA>GEORGE, Duke of Clarence</PERSONA>
<PERSONA>RICHARD, Duke of Gloucester, afterwards King Richard III.</PERSONA>
<GRPDESCR>brothers to the King.</GRPDESCR>
</PGROUP>

<PERSONA>A young son of Clarence. </PERSONA>
<PERSONA>HENRY, Earl of Richmond, afterwards King Henry VII.</PERSONA>
<PERSONA>CARDINAL BOURCHIER, Archbishop of Canterbury. </PERSONA>
<PERSONA>THOMAS ROTHERHAM, Archbishop of York. </PERSONA>
<PERSONA>JOHN MORTON, Bishop of Ely. </PERSONA>
<PERSONA>DUKE of BUCKINGHAM</PERSONA>
<PERSONA>DUKE of NORFOLK</PERSONA>
<PERSONA>EARL of SURREY, His son. </PERSONA>
<PERSONA>EARL RIVERS, Brother to Elizabeth. </PERSONA>

<PGROUP>
<PERSONA>MARQUIS OF DORSET</PERSONA>
<PERSONA>LORD GREY</PERSONA>
<GRPDESCR>Sons to Elizabeth.</GRPDESCR>
</PGROUP>

<PERSONA>EARL of OXFORD</PERSONA>
<PERSONA>LORD HASTINGS</PERSONA>
<PERSONA>LORD STANLEY, Called also EARL of DERBY. </PERSONA>
<PERSONA>LORD LOVEL</PERSONA>
<PERSONA>SIR THOMAS VAUGHAN</PERSONA>
<PERSONA>SIR RICHARD RATCLIFF</PERSONA>
<PERSONA>SIR WILLIAM CATESBY</PERSONA>
<PERSONA>SIR JAMES TYRREL</PERSONA>
<PERSONA>SIR JAMES BLOUNT</PERSONA>
<PERSONA>SIR WALTER HERBERT</PERSONA>
<PERSONA>SIR ROBERT BRAKENBURY, Lieutenant of the Tower. </PERSONA>
<PERSONA>CHRISTOPHER URSWICK, A priest. </PERSONA>
<PERSONA>Another Priest. </PERSONA>

<PGROUP>
<PERSONA>TRESSEL</PERSONA>
<PERSONA>BERKELEY</PERSONA>
<GRPDESCR>Gentlemen attending on the Lady Anne.</GRPDESCR>
</PGROUP>

<PERSONA>Lord Mayor of London. </PERSONA>
<PERSONA>Sheriff of Wiltshire. </PERSONA>
<PERSONA>ELIZABETH, Queen to King Edward IV. </PERSONA>
<PERSONA>MARGARET, Widow of King Henry VI. </PERSONA>
<PERSONA>DUCHESS of YORK, Mother to King Edward IV.</PERSONA>
<PERSONA>LADY ANNE, Widow of Edward Prince of Wales, son to King Henry VI; afterwards married to Richard.</PERSONA>
<PERSONA>A young Daughter of Clarence [MARGARET PLANTAGENET] </PERSONA>
<PERSONA>Ghosts of those murdered by Richard III., Lords and other Attendants; a Pursuivant Scrivener, Citizens, Murderers, Messengers Soldiers, &amp;c.</PERSONA>
</PERSONAE>

</play>

<play>
<TITLE>The Tempest</TITLE>
<path>tempest.xml</path>

<PERSONAE>
<TITLE>Dramatis Personae</TITLE>

<PERSONA>ALONSO, King of Naples.</PERSONA>
<PERSONA>SEBASTIAN, his brother.</PERSONA>
<PERSONA>PROSPERO, the right Duke of Milan.</PERSONA>
<PERSONA>ANTONIO, his brother, the usurping Duke of Milan.</PERSONA>
<PERSONA>FERDINAND, son to the King of Naples.</PERSONA>
<PERSONA>GONZALO, an honest old Counsellor.</PERSONA>

<PGROUP>
<PERSONA>ADRIAN</PERSONA>
<PERSONA>FRANCISCO</PERSONA>
<GRPDESCR>Lords.</GRPDESCR>
</PGROUP>

<PERSONA>CALIBAN, a savage and deformed Slave.</PERSONA>
<PERSONA>TRINCULO, a Jester.</PERSONA>
<PERSONA>STEPHANO, a drunken Butler.</PERSONA>
<PERSONA>Master of a Ship. </PERSONA>
<PERSONA>Boatswain. </PERSONA>
<PERSONA>Mariners. </PERSONA>
<PERSONA>MIRANDA, daughter to Prospero.</PERSONA>
<PERSONA>ARIEL, an airy Spirit.</PERSONA>

<PGROUP>
<PERSONA>IRIS</PERSONA>
<PERSONA>CERES</PERSONA>
<PERSONA>JUNO</PERSONA>
<PERSONA>Nymphs</PERSONA>
<PERSONA>Reapers</PERSONA>
<GRPDESCR>presented by Spirits.</GRPDESCR>
</PGROUP>

<PERSONA>Other Spirits attending on Prospero.</PERSONA>
</PERSONAE>


</play>
</shakespeare-index>

格式化用的CSS文件如下:shakespeare.css
PLAY
{
BACKGROUND-COLOR: white;
WIDTH: 100%
}
TITLE
{
COLOR: #000088;
DISPLAY: block;
FONT-SIZE: 24pt;
MARGIN-BOTTOM: 12pt;
MARGIN-LEFT: 24pt;
MARGIN-TOP: 12pt
}
FM
{
DISPLAY: block;
FONT-SIZE: 10pt;
MARGIN-BOTTOM: 18pt;
MARGIN-TOP: 18pt
}
P
{
DISPLAY: block
}
PERSONAE
{
}
PGROUP
{
DISPLAY: block;
MARGIN-BOTTOM: 18pt;
MARGIN-TOP: 18pt
}
PERSONA
{
COLOR: #000022;
DISPLAY: block;
FONT-SIZE: 14pt;
MARGIN-LEFT: 5pt
}
GRPDESCR
{
COLOR: #440000;
DISPLAY: block;
FONT-SIZE: 14pt;
MARGIN-LEFT: 24pt
}
SCNDESCR
{
COLOR: #004400;
DISPLAY: block;
FONT-SIZE: 14pt;
MARGIN-BOTTOM: 12pt;
MARGIN-LEFT: 12pt;
MARGIN-TOP: 6pt
}
PLAYSUBT
{
COLOR: #880000;
DISPLAY: block;
FONT-SIZE: 36pt;
MARGIN-BOTTOM: 18pt;
MARGIN-TOP: 36pt;
TEXT-ALIGN: center
}
INDUCT
{
DISPLAY: block;
FONT-STYLE: italic
}
ACT
{
}
SCENE
{
}
PROLOGUE
{
DISPLAY: block;
FONT-STYLE: italic
}
EPILOGUE
{
DISPLAY: block;
FONT-STYLE: italic
}
SPEECH
{
DISPLAY: block
}
SPEAKER
{
COLOR: #880000;
DISPLAY: block;
FONT-SIZE: 14pt;
MARGIN-LEFT: 12pt;
MARGIN-TOP: 12pt
}
LINE
{
DISPLAY: block;
FONT-SIZE: 12pt;
MARGIN-LEFT: 24pt
}
STAGEDIR
{
COLOR: #004400;
DISPLAY: block;
FONT-SIZE: 14pt;
MARGIN-BOTTOM: 12pt;
MARGIN-LEFT: 12pt;
MARGIN-TOP: 6pt
}
SUBTITLE
{
}
SUBHEAD
{
}
path
{
COLOR: green;
DISPLAY: block;
FONT-SIZE: 14pt;
FONT-STYLE: italic;
MARGIN-LEFT: 12pt
}

看上去好象使用CSS来显示XML很不错的,但是将来肯定是要使用XSL来显示XML文件的。




……

相关阅读