MSXML Examples

Started by José Roca, September 02, 2011, 12:12:29 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca


' ========================================================================================
' Demonstrates the use of the tagName property.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pDOMNode AS IXMLDOMNode
   LOCAL pOwner AS IXMLDOMDocument

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pDOMNode = pXmlDoc.documentElement.childNodes.item(0)
         pOwner = pDOMNode.childNodes.item(1).ownerDocument
         AfxShowMsg pOwner.documentElement.tagName
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================



' ========================================================================================
' Demonstrates the use of the tagName property.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pNodeBook AS IXMLDOMNode
   LOCAL pDOMElement AS IXMLDOMElement

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pNodeBook = pXmlDoc.selectSingleNode("//book")
         pDOMElement = pNodeBook
         AfxShowMsg pDOMElement.tagName
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the target property.
' The following example iterates through the document's child nodes. If it finds a node of
' type NODE_PROCESSING_INSTRUCTION (7), it displays the node's target.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL pDOMNode AS IXMLDOMNode
   LOCAL pPri AS IXMLDOMProcessingInstruction
   LOCAL nodeType AS LONG
   LOCAL i AS LONG

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pNodeList = pXmlDoc.childNodes
         FOR i = 0 TO pNodeList.length - 1
            pDOMNode = pNodeList.item(i)
            nodeType = pDOMNode.nodeType
            IF nodeType = %NODE_PROCESSING_INSTRUCTION THEN
               pPri = pDOMNode
               AfxShowMsg pPri.target
            END IF
         NEXT
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the text property.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pCurrNode AS IXMLDOMNode

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pCurrNode = pXmlDoc.documentElement.childNodes.item(0)
         AfxShowMsg pCurrNode.text
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the transform method.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pXslt AS IXSLTemplate
   LOCAL pXslDoc AS IXMLDOMDocument
   LOCAL pXslProc AS IXSLProcessor
   LOCAL pTemplate AS IXSLTemplate
   LOCAL vRes AS VARIANT

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
   pXslt = NEWCOM "Msxml2.XSLTemplate.6.0"
   IF ISNOTHING(pXslt) THEN EXIT FUNCTION
   pXslDoc = NEWCOM "Msxml2.FreeThreadedDOMDocument.6.0"
   IF ISNOTHING(pXslDoc) THEN EXIT FUNCTION

   TRY
      pXslDoc.async = %VARIANT_FALSE
      pXslDoc.load "sample2.xsl"
      IF pXslDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXslDoc.parseError.reason
      ELSE
         pXslt.putref_stylesheet = pXslDoc
         pXmlDoc.async = %FALSE
         pXmlDoc.load "books.xml"
         IF pXmlDoc.parseError.errorCode THEN
            AfxShowMsg "You have error " & pXmlDoc.parseError.reason
         ELSE
            pXslProc = pXslt.createProcessor
            pXslProc.input = pXmlDoc
            pXslProc.transform
            vRes = pXslProc.output
            AfxShowMsg VARIANT$$(vRes)
         END IF
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the transformNode method.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pStyleSheet AS IXMLDOMDocument

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
   pStyleSheet = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pStyleSheet) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pStyleSheet.async = %VARIANT_FALSE
         pStyleSheet.load "sample2.xsl"
         IF pStyleSheet.parseError.errorCode THEN
            AfxShowMsg "You have error " & pStyleSheet.parseError.reason
         ELSE
            AfxShowMsg pXmlDoc.transformnode(pStyleSheet)
         END IF
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the transformNodeToObject method.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pSource AS IXMLDOMDocument
   LOCAL pStylesheet AS IXMLDOMDocument
   LOCAL pStylesheet2 AS IXMLDOMDocument
   LOCAL pResult AS IXMLDOMDocument
   LOCAL pResult2 AS IXMLDOMDocument

   pSource = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pSource) THEN EXIT FUNCTION
   pStylesheet = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pStylesheet) THEN EXIT FUNCTION
   pStylesheet2 = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pStylesheet2) THEN EXIT FUNCTION
   pResult = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pResult) THEN EXIT FUNCTION
   pResult2 = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pResult2) THEN EXIT FUNCTION

   TRY
      pSource.async = %VARIANT_FALSE
      pSource.load "sample.xml"
      IF pSource.parseError.errorCode THEN
         AfxShowMsg "You have error " & pSource.parseError.reason
      ELSE
         pStyleSheet.async = %VARIANT_FALSE
         pStyleSheet.load "stylesheet1.xsl"
         IF pStyleSheet.parseError.errorCode THEN
            AfxShowMsg "You have error " & pStyleSheet.parseError.reason
         ELSE
            pResult.async = %VARIANT_FALSE
            pResult.validateOnParse = %VARIANT_TRUE
            pResult2.async = %VARIANT_FALSE
            pResult2.validateOnParse = %VARIANT_TRUE
            pSource.transformNodeToObject pStylesheet, pResult
            pStyleSheet2.async = %VARIANT_FALSE
            pStyleSheet2.load "stylesheet2.xsl"
            IF pStyleSheet2.parseError.errorCode THEN
               AfxShowMsg "You have error " & pStyleSheet2.parseError.reason
            ELSE
               pResult.transformNodeToObject pStylesheet2, pResult2
               AfxShowMsg pResult2.xml
            END IF
         END IF
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the url property (IXMLDOMDocument).
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISFALSE ISOBJECT(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         AfxShowMsg pXmlDoc.url
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the validate method.
' When used with MSXML 6.0, you should get the following output:
' Invalid Dom: Element 'review' is unexpected according to content model of parent
' element 'book'
' Expecting: price.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL i AS LONG
   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pParseError AS IXMLDOMParseError
   LOCAL pSchemaCache AS IXMLDOMSchemaCollection
   LOCAL pNodeList AS IXMLDOMNodeList

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
   pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.6.0"
   IF ISNOTHING(pSchemaCache) THEN EXIT FUNCTION

   pSchemaCache.add "urn:books", "validateNode.xsd"
   pXmlDoc.putref_schemas = pSchemaCache
   pXmlDoc.validateOnParse = %VARIANT_FALSE
   pXmlDoc.async = %VARIANT_FALSE
   pXmlDoc.load "validateNode.xml"
   pParseError = pXmlDoc.validate
   IF OBJRESULT = %S_FALSE THEN
      AfxShowMsg "Invalid DOM - Error &H: " & HEX$(pParseError.errorCode, 8)
      AfxShowMsg pParseError.reason
   ELSEIF OBJRESULT = %E_PENDING THEN
      AfxShowMsg "The document is not completely loaded."
   ELSEIF OBJRESULT <> %S_OK THEN
      AfxShowMsg "Error &H" & HEX$(OBJRESULT)
   ELSE
      AfxShowMsg "DOM is valid: " & pXmlDoc.xml
      pNodeList = pXmlDoc.selectNodes("//book")
      FOR i = 0 TO pNodeList.length - 1
         AfxShowMsg pNodeList.item(i).xml
      NEXT
   END IF

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the validateNode method.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument3
   LOCAL pParseError AS IXMLDOMParseError
   LOCAL pSchemaCache AS IXMLDOMSchemaCollection
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL pNode AS IXMLDOMNode
   LOCAL i AS LONG

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION
   pSchemaCache = NEWCOM "Msxml2.XMLSchemaCache.6.0"
   IF ISNOTHING(pSchemaCache) THEN EXIT FUNCTION

   pSchemaCache.add "urn:books", "validateNode.xsd"
   pXmlDoc.putref_schemas = pSchemaCache
   pXmlDoc.validateOnParse = %VARIANT_FALSE
   pXmlDoc.async = %VARIANT_FALSE

   pXmlDoc.load "validateNode.xml"
   IF pXmlDoc.parseError.errorCode THEN
      AfxShowMsg "You have error " & pXmlDoc.parseError.reason
   ELSE
      pNodeList = pXmlDoc.selectNodes("//book")
      FOR i = 0 TO pNodeList.length - 1
         pNode = pNodeList.item(i)
         pParseError = pXmlDoc.validateNode(pNode)
         IF OBJRESULT = %S_FALSE THEN
            AfxShowMsg "Invalid node - Error &H" & HEX$(pParseError.errorCode, 8)
            AfxShowMsg pParseError.reason
         ELSEIF OBJRESULT <> %S_OK THEN
            AfxShowMsg "Error &H" & HEX$(OBJRESULT)
         ELSE
            AfxShowMsg pNode.xml
         END IF
         pParseError = NOTHING
         pNode = NOTHING
      NEXT
   END IF

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the validateOnParse property.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISFALSE ISOBJECT(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.validateOnParse = %VARIANT_TRUE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         AfxShowMsg "Validated"
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the value property.
' The following example gets the value of the first attribute of the document root and
' assigns it to the variable vValue.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pRootNode AS IXMLDOMElement
   LOCAL pCurrNode AS IXMLDOMNode
   LOCAL pNamedNodeMap AS IXMLDOMNamedNodeMap
   LOCAL pDOMNode AS IXMLDOMNode
   LOCAL pDOMAttr AS IXMLDOMAttribute
   LOCAL vValue AS VARIANT

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pRootNode = pXmlDoc.documentElement
         pCurrNode = pRootNode.firstChild
         pNamedNodeMap = pXmlDoc.documentElement.firstChild.attributes
         pDOMNode = pNamedNodeMap.item(0)
         pDOMAttr = pDOMNode
         vValue = pDOMAttr.value
         AfxShowMsg VARIANT$$(vValue)
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca


' ========================================================================================
' Demonstrates the use of the xml property.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument
   LOCAL pCurrNode AS IXMLDOMNode

   pXmlDoc = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.load "books.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pCurrNode =  pXmlDoc.documentElement.childNodes.Item(0)
         AfxShowMsg pCurrNode.xml
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


José Roca

 
The XPath implementation in MSXML 4.0 introduces a set of XSD extension functions that can specify expressions that evaluate nodes in an XML document based on their data type. The ms:type-local-name XPath extension function can return the non-qualified name of a node's data type as defined in an associated XSD schema.


' ========================================================================================
' Demonstrates the use of the ms:type-local-name XPath Extension function when
' programming the MSXML DOM.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pParseError AS IXMLDOMParseError
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL pNode AS IXMLDOMNode
   LOCAL i AS LONG

   ' only works with version 4.0
   pXmlDoc = NEWCOM "Msxml2.DOMDocument.4.0"
   IF ISFALSE ISOBJECT(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"
      pXmlDoc.load "books3.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pNodeList = pXmlDoc.selectNodes("//*[ms:type-local-name(.)='AuthorType']")
         FOR i = 0 TO pNodeList.length - 1
            AfxShowMsg pNodeList.item(i).nodeName
         NEXT
         pNodeList = NOTHING
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


The following example uses an XSLT template rule to select from books.xml all the elements and to output the elements data types and the namesapce URI as defined in books.xsd.


' ========================================================================================
' Demostrates the use of the ms:type-namespace-uri XPath Extension function.
' The following example uses an XSLT template rule to select from books.xml all the
' elements and to output the elements data types and the namesapce URI as defined in
' books.xsd.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pxsd AS IXMLDOMSchemaCollection
   LOCAL pxml AS IXMLDOMDocument2
   LOCAL pxsl AS IXMLDOMDocument2

   pxsd = NEWCOM "Msxml2.XMLSchemaCache.6.0"
   IF ISNOTHING(pxsd) THEN EXIT FUNCTION
   pxml = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pxml) THEN EXIT FUNCTION
   pxsl = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pxsl) THEN EXIT FUNCTION

   ' namespace uri ("urn:books") must be declared as one of the namespace
   ' declarations in the "books.xml" that is an instance of "books.xsd"
   pxsd.add "urn:books", "books5.xsd"

   pxml.putref_schemas = pxsd
   pxml.setProperty "SelectionLanguage", "XPath'"
   pxml.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"

   pxml.async = %VARIANT_FALSE
   pxml.validateOnParse = %VARIANT_TRUE
   pxml.load "books5.xml"

   pxsl.async = %VARIANT_FALSE
   pxsl.load "books5.xslt"
   AfxShowMsg pxml.transformNode(pxsl)

END FUNCTION
' ========================================================================================


José Roca

 
The XPath implementation in MSXML version 4.0 introduces a set of XML Schema Definition (XSD) extension functions that can specify expressions that evaluate nodes in an XML document based on the data type of the nodes. The ms:type-namespace-uri XPath extension function can return the namespace Uniform Resource Identifier (URI) that is associated with the XSD data type of the current node, or the first node in the node set that is provided as its input parameter. This function can run XPath queries in MSXML 4.0 DOM code to identify nodes whose data types are associated with a specified namespace URI. This article provides a code sample to demonstrate how to use the ms:type-namespace-uri XPath extension function when you program the MSXML 4.0 DOM in PowerBASIC.


' ========================================================================================
' Demonstrates the use of the ms:type-namespace-uri XPath Extension function when
' programming the MSXML DOM.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pXmlDoc AS IXMLDOMDocument2
   LOCAL pParseError AS IXMLDOMParseError
   LOCAL pNodeList AS IXMLDOMNodeList
   LOCAL i AS LONG

   ' only works with version 4.0
   pXmlDoc = NEWCOM "Msxml2.DOMDocument.4.0"
   IF ISNOTHING(pXmlDoc) THEN EXIT FUNCTION

   TRY
      pXmlDoc.async = %VARIANT_FALSE
      pXmlDoc.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"
      pXmlDoc.load "books3.xml"
      IF pXmlDoc.parseError.errorCode THEN
         AfxShowMsg "You have error " & pXmlDoc.parseError.reason
      ELSE
         pNodeList = pXmlDoc.selectNodes("//*[ms:type-namespace-uri()='urn:books']")
         FOR i = 0 TO pNodeList.length - 1
            AfxShowMsg pNodeList.item(i).nodeName
         NEXT
         pNodeList = NOTHING
      END IF
   CATCH
      AfxShowMsg OleGetErrorInfo(OBJRESULT)
   END TRY

END FUNCTION
' ========================================================================================


The following example uses an XSLT template rule to select from books.xml all the elements and to output the elements data types and the namesapce URI as defined in books.xsd.


' ========================================================================================
' Demonstrates the use of the ms:type-namespace-uri XPath Extension function.
' The following example uses an XSLT template rule to select from books.xml all the
' elements and to output the elements data types and the namesapce URI as defined in
' books.xsd.
' ========================================================================================

#DIM ALL
#COMPILE EXE
#INCLUDE ONCE "msxml.inc"
#INCLUDE ONCE "ole2utils.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pxsd AS IXMLDOMSchemaCollection
   LOCAL pxml AS IXMLDOMDocument2
   LOCAL pxsl AS IXMLDOMDocument2

   pxsd = NEWCOM "Msxml2.XMLSchemaCache.6.0"
   IF ISNOTHING(pxsd) THEN EXIT FUNCTION
   pxml = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pxml) THEN EXIT FUNCTION
   pxsl = NEWCOM "Msxml2.DOMDocument.6.0"
   IF ISNOTHING(pxsl) THEN EXIT FUNCTION

   ' namespace uri ("urn:books") must be declared as one of the namespace
   ' declarations in the "books.xml" that is an instance of "books.xsd"
   pxsd.add "urn:books", "books4.xsd"

   pxml.putref_schemas = pxsd
   pxml.setProperty "SelectionLanguage", "XPath'"
   pxml.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"

   pxml.async = %FALSE
   pxml.validateOnParse = %TRUE
   pxml.load "books4.xml"

   pxsl.async = %FALSE
   pxsl.load "books4.xslt"
   AfxShowMsg pxml.transformNode(pxsl)

END FUNCTION
' ========================================================================================