472,102 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

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

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
1 3526
Dormilich
8,658 Expert Mod 8TB
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.

Similar topics

1 post views Thread by ahaideb | last post: by
2 posts views Thread by john smith | last post: by
4 posts views Thread by Matteo Migliore | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.