我如何导航到父属性(xmlproperty) - codessmith
本文关键字:属性 xmlproperty codessmith 导航 何导航 | 更新日期: 2023-09-27 18:11:16
我有以下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>
我有以下代码模板:
<%@ 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 %>
当我循环遍历Items时,我需要打印Address' city。本质上,我如何遍历到父节点(或父节点的父节点)?
在您的示例中更改这一行:
//Here I need to traverse to the parent and print the City
:
<%= MyPurchaseOrder.ShipTo.City %>
由于您使用的所有"路径"都是绝对的(即从MyPurchaseOrder
开始),因此在这种情况下应该可以使用绝对路径(您不需要导航到父节点)