How do I navigate to the parent property (xmlproperty) - Codesmith

127 Views Asked by At

I have the following xml:

<?xml version="1.0"?>
<PurchaseOrder xmlns="http://www.codesmithtools.com/purchaseorder">
  <ShipTo Name="Eric J. Smith">
    <Line1>123 Test Dr.</Line1>
    <City>Dallas</City>
    <State>TX</State>
    <Zip>75075</Zip>
  </ShipTo>
  <OrderDate>05-01-2003</OrderDate>
  <Items>
    <OrderedItem>
      <ItemName>Item #1</ItemName>
      <Description>Item #1 Description</Description>
      <UnitPrice>5.45</UnitPrice>
      <Quantity>3</Quantity>
      <LineTotal>16.35</LineTotal>
    </OrderedItem>
    <OrderedItem>
      <ItemName>Item #2</ItemName>
      <Description>Item #2 Description</Description>
      <UnitPrice>12.75</UnitPrice>
      <Quantity>8</Quantity>
      <LineTotal>102.00</LineTotal>
    </OrderedItem>
  </Items>
  <SubTotal>45.23</SubTotal>
  <ShipCost>5.23</ShipCost>
  <TotalCost>50.46</TotalCost>
</PurchaseOrder>

I have the following codesmith template:

    <%@ XmlProperty Name="MyPurchaseOrder" Schema="PurchaseOrder.xsd" Default="SamplePurchaseOrder.xml" %>

    PurchaseOrder:
        Address:
            Name: <%= MyPurchaseOrder.ShipTo.Name %>
            Line1: <%= MyPurchaseOrder.ShipTo.Line1 %>
            City: <%= MyPurchaseOrder.ShipTo.City %>
            State: <%= MyPurchaseOrder.ShipTo.State %>
            Zip: <%= MyPurchaseOrder.ShipTo.Zip %>
        OrderDate: <%= MyPurchaseOrder.OrderDate %>
        Items:
            <% for (int i = 0; i < MyPurchaseOrder.Items.Count; i++) { %>
            <%= i %>:
                ItemName: <%= MyPurchaseOrder.Items[i].ItemName %>
//Here I need to traverse to the parent and print the City
                Description: <%= MyPurchaseOrder.Items[i].Description %>
                UnitPrice: <%= MyPurchaseOrder.Items[i].UnitPrice %>
                Quantity: <%= MyPurchaseOrder.Items[i].Quantity %>
                LineTotal: <%= MyPurchaseOrder.Items[i].LineTotal %>
            <% } %>
        SubTotal: <%= MyPurchaseOrder.SubTotal %>
        ShipCost: <%= MyPurchaseOrder.ShipCost %>
        TotalCost: <%= MyPurchaseOrder.TotalCost %>

As I loop through the Items, I need to print the Address' city. In essence, how do I traverse to the parent (or a parent's parent)?

1

There are 1 best solutions below

1
On

Change this line in your example:

//Here I need to traverse to the parent and print the City

to this:

<%= MyPurchaseOrder.ShipTo.City %>

Since all of the 'paths' you are using are absolute (i.e. starting from MyPurchaseOrder), it should be possible to use an absolute path in this case (you don't need to navigate up to parent nodes).