By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,122 Members | 654 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,122 IT Pros & Developers. It's quick & easy.

Unable to add Parent node name(if any) to a child node XSLT 1.0

P: 1
I will very helpful to you if you can help me out in my below problem. I am not getting idea what should be the right approach for this.
Here is my problem.

I have below (sample)xml.

Expand|Select|Wrap|Line Numbers
  1. <Rules>
  2. <MapRule input="@PARENT()" output="Sales_Totals" />
  3. <MapRule input="''" output="Sales_Totals@name" />
  4. <MapRule input="''" output="Sales_Totals@type" />
  5. <MapRule input="" output="Sales_Totals@allTimeSales" />
  6. <MapRule input="@PARENT()" output="ProductList" />
  7. <MapRule input="" output="ProductList@year" />
  8. <MapRule input="" output="ProductList@value" />
  9. <MapRule input="@PARENT()" output="Product" />  ( Product node is getting created here)
  10. <MapRule input="" output="Product@quarter" />    ("quarter" is an attribue of Product node)
  11. <MapRule input="" output="Name" />            (PRoduct node has three leaf nodes - Name , Value, Sold)    
  12. <MapRule input="" output="Value" />
  13. <MapRule input="" output="Sold" />
  14. <MapRule input="@ENDPARENT()" output="Product" /> (Product node is getting closed here)
  15. <MapRule input="" output="Services" />
  16. <MapRule input="" output="Services@lawnservice" />
  17. <MapRule input="@ENDPARENT()" output="ProductList"/>
  18. <MapRule input="@ENDPARENT()" output="Sales_Totals"/>
  19. </Rules>
In this xml , if the attribute "output" of the node "MapRule" contains "@" symbol then it is not a node name but an attribute of a node, otherwise node name.

Ex:
a) <MapRule input="@PARENT()" output="Sales_Totals" />
Here Sales_total is a node name (i.e, it is not a leaf node).

b) <MapRule input="''" output="Sales_Totals@name" />
Here "name" is an attribute of "Sales_Totals" node.

c)<MapRule input="" output="Name" />
Here attribute "input" is empty and attribute "output" does not have @ symbol , it means it is a leaf node.


I want to display the nodes and the attributes(should be followed by @ symbol) in the below way.
Expand|Select|Wrap|Line Numbers
  1. Sales_total 
  2. Sales_Total@name
  3. Sales_Total@type
  4. Sales_total@allTimeSales
  5. Sales_TotalProductList (<- From here my problem area. )
  6. Sales_TotalProductList@year
  7. Sales_TotalProductList@value
  8. Sales_TotalProductListProduct
  9. Sales_TotalProductListProduct@Quarter
  10. Sales_TotalProductListProductName
  11. Sales_TotalProductListProductValue
  12. Sales_TotalProductListProductSold
  13. Sales_TotalProductListServices
  14. Sales_TotalProductListServices@lawnservice
I first thought of creating a variable and assigning the parent node to it. For ex var xyz = "Sales_node". But this is not right because i cannot modify xyz with Sales_node/ProductList. And this is changing again and again. Appreciate if any one can help me in this.

Thanks
Aug 27 '09 #1
Share this Question
Share on Google+
1 Reply

Dormilich
Expert Mod 5K+
P: 8,639
to me it looks as if your XML is overcomplicating matters. after reading several times it looks as if it is a kind of XML definition.

point 1: to describe an XML fileís structure you should use XML Schema or a DTD (there are some more, but these are the most common)

point 2: if you donít want to (or canít) use those definition languages, at least structure your definition XML. this will make it easier to recognise parent-child-relations. this will also help applying XSLT/XPath.

ex.
Expand|Select|Wrap|Line Numbers
  1. <element name="Sales_Totals">
  2.   <attribute name="name"/>
  3.   <element name="ProductList">
  4.     <element name="Product"/>
  5.   </element>
  6. </element>
Note: a node in XML can be anything, a coarse grouping would be element node (thatís what I think you call ďleaf nodeĒ), attribute node, text node & namespace node (there are around a dozen more node types)

PS. welcome @ Bytes.com
Sep 2 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.