Understanding XML

Worked Example: XML and XPaths

104 views October 24, 2018 October 24, 2018 admin 0

For more information introducing key XML concepts please read our Introducing XML files guide.

Sample catalog layout

 

Sample catalog XML

The following XML file is typical of the data required to create the simple catalog table shown.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Catalog CategoryName="Sample">
<!-- Catalog structure -->
	<MainCategory CategoryName="Switches &amp; socket outlets" CategoryID="101" CategorySortOrder="1">
		<Category CategoryName="Simplo light switches" CategoryID="202" CategorySortOrder="1">
			<SubCategory CategoryName="Easy use light switches" CategoryID="301" CategorySortOrder="1">
<!-- Product records -->
				<Products>
				
					<Product ProductInstanceID="123" ProductCode="E5145" ProductSortOrder="1">
						<ProductAttributes>
							<Description>Rocker switch, single, 10A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>10</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="124" ProductCode="E5150" ProductSortOrder="2">
						<ProductAttributes>
							<Description>Rocker switch, double, 10A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>10</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="125" ProductCode="E5155" ProductSortOrder="3">
						<ProductAttributes>
							<Description>Rocker switch, triple, 10A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>10</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="126" ProductCode="E5160" ProductSortOrder="4">
						<ProductAttributes>
							<Description>Rocker switch, single, 15A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>15</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="127" ProductCode="E5165" ProductSortOrder="5">
						<ProductAttributes>
							<Description>Rocker switch, double, 15A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>15</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="128" ProductCode="E5170" ProductSortOrder="6">
						<ProductAttributes>
							<Description>Rocker switch, triple, 15A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>15</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>

				</Products>

			</SubCategory>
			<SubCategory CategoryName="Rocker switches" CategoryID="302" CategorySortOrder="2">
				<CategoryText>These switches really are awesome. Easy to fix, easy to clean, with groovy coloured inserts.</CategoryText>
<!-- Product records -->
				<Products>
				
					<Product ProductInstanceID="223" ProductCode="E5145" ProductSortOrder="1">
						<ProductAttributes>
							<Description>Single switch, 10A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>10</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="224" ProductCode="E5150" ProductSortOrder="2">
						<ProductAttributes>
							<Description>Single switch, 15A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>15</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="225" ProductCode="E5155" ProductSortOrder="3">
						<ProductAttributes>
							<Description>Double switch, 10A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>10</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="226" ProductCode="E5160" ProductSortOrder="4">
						<ProductAttributes>
							<Description>Double switch, 15A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>15</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="227" ProductCode="E5165" ProductSortOrder="5">
						<ProductAttributes>
							<Description>Triple switch, 10A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>10</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="228" ProductCode="E5170" ProductSortOrder="6">
						<ProductAttributes>
							<Description>Triple switch, 15A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>15</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>
					<Product ProductInstanceID="229" ProductCode="E5175" ProductSortOrder="7">
						<ProductAttributes>
							<Description>Single switch, 10A</Description>
							<Colour>WH</Colour>
							<Voltage>240</Voltage>
							<Amps>10</Amps>
							<Pricing RRP="6.13" Trade="3.68" />
						</ProductAttributes>
					</Product>

				</Products>

			</SubCategory>
		</Category>
	</MainCategory>
</Catalog>

XPath Reference

Working sequentially down the XML file, the following fields would need to be created within the corresponding EasyCatalog panel.

The XPaths shown are relative to the /Catalog/MainCategory/Category/SubCategory/Products/Product record XPath.

Field Name XPath Sample Data
MainCategoryName ../../../../@CategoryName Switches & socket outlets
MainCategoryID ../../../../@CategoryID 101
MainCategorySortOrder ../../../../@CategorySortOrder 1
CategoryName ../../../@CategoryName Simplo light switches
CategoryID ../../../@CategoryID 201
CategorySortOrder ../../../@CategorySortOrder 1
SubCategoryName ../../@CategoryName Easy use light switches
SubCategoryID ../../@CategoryID 301
SubCategorySortOrder ../../@CategorySortOrder 1
ProductInstanceID @ProductInstanceID 123
ProductCode @ProductCode E5145
ProductSortOrder @ProductSortOrder 1
Description ProductAttributes/Description/text() Rocker switch, single, 10A
Colour ProductAttributes/Colour/text() WH
Voltage ProductAttributes/Voltage/text() 240
Amps ProductAttributes/Amps/text() 10
RRP ProductAttributes/Pricing/@RRP 6.13
Trade ProductAttributes/Pricing/@Trade 3.68

 

Was this helpful?