Re: Random access of XML file

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Stuart Celarier (stuart)
Date: 11/14/04

  • Next message: Victor Hadianto: "Re: Sending Word Document as an attachement."
    Date: Sat, 13 Nov 2004 18:10:41 -0800
    
    

    As Martin suggested, you can use the XmlNode.SelectNodes method [1] to
    select all nodes matching an XPath expression [2]. XmlNode.SelectSingleNode
    selects the first node (in document order) that matches the XPath
    expression.

    The XmlDocument.GetElementByID method requires use of a DTD to identify
    which attributes are treated as IDs [3]. DTDs are very nearly dead, and you
    are better served in the long term by not relying on them.

    If your document is not well-formed XML, then all bets are off.
    Fundamentally, if it's not well-formed XML, then it isn't XML, period. In
    the case of XmlDocument, you can't load a document that is not well-formed
    XML.

    If you want to go a step further beyond XPath support (SelectNodes), you can
    look at XSLT [4], but that may be overkill depending on your needs. The XSLT
    element <xsl:key> acts like an index over the XML source document, and the
    key() function acts like a SELECT statement over that index. This has the
    potential of delivering better performance.

    Here is a key that indexes all elements having at least one attribute whose
    name contains the string 'ID', indexed by the values of any such attributes.

    <xsl:key name="AllIDs"
        match="*[contains(local-name(@*), 'ID')]"
        use="@*[contains(local-name(.), 'ID')]"/>

    That key can be used in expressions in XSLT elements like this

    key('AllIDs', '3')

    This selects all elements anywhere in the document that have at least one
    attribute whose name contains the string 'ID' and whose value is 3. (Caveat,
    I'm just writing this XSLT off the top of my head to give you the flavor.)

    I should point out that XSLT is a general purpose transform language for XML
    and can take a while to learn. It may be much more than you need or are
    willing to undertake.

    Cheers,
    Stuart Celarier, Fern Creek

    [1]
    http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemxmlxmlnodeclassselectnodestopic1.asp
    [2] http://www.w3.org/TR/1999/REC-xpath-19991116
    [3]
    http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemxmlxmldocumentclassgetelementbyidtopic.asp
    [4] http://www.w3.org/TR/1999/REC-xslt-19991116


  • Next message: Victor Hadianto: "Re: Sending Word Document as an attachement."

    Relevant Pages

    • Re: XSLT: Xpath with variables
      ... XPath and XSLT don't support dynamic evaluation ... of XPath expressions, so you task at ones becomes quite onerous just ... traditional XML means to express relationship - hierarchy, ... I'd go for two-step transformation - first generate XSLT stylesheet ...
      (microsoft.public.dotnet.xml)
    • Re: Tool to create XSLT templates
      ... curious design choice. ... XSLT 2 and XPath F & O provide powerful text- ... XML, to XML or HTML or plain text. ...
      (comp.text.xml)
    • Re: xml simple question
      ... The trick is that i dont know the exact stucture of the xml ... The primary purpose of XPath is to address parts of an XML ... Depending on what you want to do with the XML data, XSLT may also help: ... language for transforming XML documents into other XML documents. ...
      (perl.beginners)
    • Re: XML Standard
      ... >> very important standards which relate to manipulating XML documents are ... While XSLT contains some imperative programming ... > standards for XPATH and/or XSLT? ... Since there is formal liaison with ISO on XML (as well as other web-related ...
      (comp.lang.cobol)
    • Re: Using PHP to parse specific XML tag content?
      ... XML --> HTML transformation in testing environment. ... run just fine under Windows using Cygwin, and, unless I'm ... but you have to learn XSLT or use whatever defaults ...
      (comp.lang.php)