Author Topic: MSXML Examples  (Read 91747 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: tagName Property
« Reply #135 on: September 02, 2011, 11:26:34 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: target Property
« Reply #136 on: September 02, 2011, 11:27:26 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: text Property
« Reply #137 on: September 02, 2011, 11:28:06 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: transform Method
« Reply #138 on: September 02, 2011, 11:30:45 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: transformNode Method
« Reply #139 on: September 02, 2011, 11:31:17 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: transformNodeToObject Method
« Reply #140 on: September 02, 2011, 11:32:02 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: url Property (DOMDocument)
« Reply #141 on: September 02, 2011, 11:32:43 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: validate Method
« Reply #142 on: September 02, 2011, 11:33:15 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: validateNode Method
« Reply #143 on: September 02, 2011, 11:33:54 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: validateOnParse Property
« Reply #144 on: September 02, 2011, 11:34:36 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: value Property
« Reply #145 on: September 02, 2011, 11:35:18 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: xml Property
« Reply #146 on: September 02, 2011, 11:35:50 AM »
Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: How to use the ms:type-local-name XPath extension function
« Reply #147 on: September 02, 2011, 11:44:28 AM »
 
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.

Code: [Select]
' ========================================================================================
' 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.

Code: [Select]
' ========================================================================================
' 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
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
MSXML: How to use the ms:type-namespace-uri XPath extension function
« Reply #148 on: September 02, 2011, 11:46:24 AM »
 
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.

Code: [Select]
' ========================================================================================
' 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.

Code: [Select]
' ========================================================================================
' 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
' ========================================================================================