VB XML Cookbook, Recipe 2: Descendants and Ancestors (Doug Rothaus)

VBTeam

This entry in the cookbook shows how you can access descendant and ancestor elements in an XML document using Visual Basic, XML Axis properties, and LINQ to XML objects.

Descendants

Visual Basic provides XML Axis properties that enable you to easily refer to child nodes and attributes. As is often the case with XML, you may need to reference sub-elements that show up in different levels of the XML hierarchy. In that case, you can use the XML Descendant axis property.

The XML Descendant axis property is identified using three periods followed by the XML element that you want to refer to. For example, in the AdventureWorks contacts source document that we used in Recipe 1 (you can download the XML document and related schemas from the Recipe 1 post), there is an <AdditionalContactInfo> element that contains information about the contact such as phone numbers, shipping and billing addresses, and so on. The phone numbers in the content of the <AdditionalContactInfo> element can show up anywhere within the value of the element. As a result, you would need to use the XML Descendant axis property to refer to the phone number elements. For example:

Dim xmlDoc = XDocument.Load(“AWContacts.xml”)

 

‘ Return all descendant <telephoneNumber> elements

Dim telephoneNumbers = _

  xmlDoc.<Contacts>.<Contact>.<aci:AdditionalContactInfo><act:telephoneNumber>

 

The XML descendant axis property returns a collection of all of the matching elements “down” the XML hierarchy. Let’s look at a more complete example. The following Function returns a list of contact names and phone numbers using the XML Descendant axis property.

Imports <xmlns=http://SampleSchema/AWContacts>

Imports <xmlns:s=sample-output>

Imports <xmlns:aci=http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo>

Imports <xmlns:act=http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes>

 

Public Class Recipe2

 

  Public Function GetPhoneNumbers(ByVal xmlDoc As XDocument) As XElement

    Return <s:ContactPhoneNumbers>

             <%= From contact In xmlDoc.<Contacts>.<Contact> _

                 Where contact.<aci:AdditionalContactInfo><act:telephoneNumber>.<act:number>.Count > 0 _

                 Select <s:Contact>

                          <s:Name><%= contact.<FirstName>.Value & ” “ & _

                                      contact.<LastName>.Value %></s:Name>

                          <s:PhoneNumbers>

                            <%= From number In _

                                contact.<aci:AdditionalContactInfo><act:telephoneNumber>.<act:number> _

                                Select <s:Number><%= number.Value %></s:Number> _

                            %>

                          </s:PhoneNumbers>

                        </s:Contact> _

             %>

           </s:ContactPhoneNumbers>

  End Function

End Class

In XSLT, the same transform would look like the following:

<?xml version=1.0?>

<xsl:stylesheet

  version=1.0

  xmlns:xsl=http://www.w3.org/1999/XSL/Transform

  xmlns:aw=http://SampleSchema/AWContacts

  xmlns:s=sample-output

  xmlns:aci=http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo

  xmlns:crm=http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecord

  xmlns:act=http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes>

<xsl:output method=xml indent=yes />

 

  <xsl:template match=aw:Contacts>

    <s:ContactPhoneNumbers>

      <xsl:apply-templates select=aw:Contact />

    </s:ContactPhoneNumbers>

  </xsl:template>

 

  <xsl:template match=aw:Contact>

    <xsl:if test=count(aci:AdditionalContactInfo//act:telephoneNumber/act:number) > 0>

      <s:Contact

0 comments

Leave a comment

Feedback usabilla icon