Skip to content

Instantly share code, notes, and snippets.

@jsheridanwells
Last active May 4, 2018 20:23
Show Gist options
  • Save jsheridanwells/0b4bb64ce98f6cb5e13652711ecd46b0 to your computer and use it in GitHub Desktop.
Save jsheridanwells/0b4bb64ce98f6cb5e13652711ecd46b0 to your computer and use it in GitHub Desktop.
XML Notes

XML Notes

XML - eXtensible: you can make up your own tags

Foundation of several technologies, XHTML, RSS/ATOM, AJAX, APIs

Structures and describes information

Intended to be used a an Internet technologies

XML can help different apps communicate.

XPath - navigates data in XML - XSLT - style transformations - XQuery queries XML - XPointer links between XML docs

Describing data

Jeremy 555-1212 555-1212

tags describe the data, the data is between the tags

XML is not good for large datasets - XML is not as good as JSON in javascript - Doesn't represent binary data very well - Note easy to read

Basic Rules

Types:

  • Document declarations - (optional, but recommended)
  • Element and attributes - (tags), need valid names, cannot use "xml" in string,
  • Comments - embed information for reader
  • Character data - dta not parsed , can be used to embed scripts
  • Processing Instructions -
  • Entity references - shorten XML documents, character entities < or &

Syntax:

  • must have a single root tag
  • must be well-formed
  • empty tags must be closed with /> (like an img tag)
  • attribute tags cannot be minimized, values must be in quotes
  • tags need to be properly nested

Valid xml:

  • DTDs, and XML Schema

Namespaces:

  • keep tag names from coliding.

Styling with XSL

To associate XML with CSS: <?xml-stylesheet type="text/css" href="FirstXMLFile.css" ?>

Using the DOM to manipulate XML data

nodeName -> name of node

nodeType -> type (element, tag, etc.)

nodeValue -> values

attributes -> elements only, an array of attributes and values

parentNode -> gives you parent of node, null if oyu're at root

childNodes -> array of child notes, array#length == 0 if there are no nodes

firstChild lastChild -> first and last child node.

previousSibling nextSibling -> self-explanatory.

Functions:

appendChild()

removeChild()

insertBefore()

replaceChild()

documentElement -> references root doc element

createElement() ->

createTextNode() ->

createComment()

createCDATASection()

getElementsByTagName()

getElementsById()

getAttribute()

setAttribute()

removeAttribute()

XPath

/html/head/title

/html/body/p[1]

(Xpath is 1-based)

Context node - where xpath query starts

Axis is relation between context and selected node

Predicates are further refinements of XPath

/ - root

/rootTag - selects

'//tagName - finds all tagName(s)

/doc/chapter[5] selects fifth chapter in doc

/body/p[last()] selects last p in body

/body/p[@class='a'] se;ects p with class of a in body

//p[@class and @style] all p in doc with class and style

XSLT

Creating a stylesheet, use: <xsl:stylesheet > rfoot tag

contain <xsl:template> tags which are matched to XML tags in the source data.

template contents replace source data

components:

<xsl:stylesheet> - defines document as XSL

<xsl:template match="myXPath"> - template matches data xpath

<xsl:value-of select="xpath"> - xsl can be called on data like a function

<xsl:attribute>

<xsl:text>

<xsl:foreach select="xpath">

<xsl:if test="condition">

<xsl:choose> <xsl:when> <xsl:otherwise> <xsl:sort select="">

To associated an XML doc with XSLT:

<?xml-stylesheet type="text/xsl" href="path_to/myXml.xml">

XML Schema

declared using <xsd:element>

xsd:string

xsd:boolean

xsd:integer

xsd:decimal

xsd:positiveInteg

xsd:negativeInteg

xsd:anyUri

xsd:date

xsd:time

xsd:dateTime

xsd:gMonth (gregorigan month) / etc... /

Example: <xsd:element name="MyElement" type="xsd:string"> is restricted to being a string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment