I want to create a group of elements which contain certain values.
Then I want to name this group. Finally, I want to refer this group.
Something like this:
<catalog type="expensive">
<powder>25</powder>
<cream>110</cream>
</catalog>
<catalog type="normal">
<powder>5</powder>
<cream>45</cream>
</catalog>
<person>
<name>Jim</name>
<catalog type="expensive">
<powder>25</powder>
<cream>110</cream>
</catalog>
</person>
Instead of repeating expensive catalog with every person element, is there a way for person element to refer to already created expensive catalog group?
The short answer is that you already did it in your example – if
typeuniquely identifies the catalog you can write something like:and you are done – the information you need is all there. If
typeis not unique you could add another unique attribute to identify each catalog (typicallyid).The longer answer would require some details on how you want to process and use this XML – if you want to transform it in some way using XSLTs then the above approach is fine – you can access the catalog of each person using something like this:
assuming the current node is the person element and that all the catalog definitions are in the same XML just under the root.