473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XSL Transform Help Please

I am having a problem with writing an XSL Transform. Please help.

Note: I know this a long post but I wanted to provide as must detail as
possible. Any help would be appreciated. (Just started writing my
first transform yesterday and no one at my company knows this stuff.)

Assume I have the following XML:

<P C="P" O="1" >
<E C="P_E1" O="1" W='True'>
<A C="P_E1A1" V="1" />
<A C="P_E1A1" V="2" />
</E>
<E C="P_E10" O="13" W='True'>
<A C="P_E10A1" V="25" />
<A C="P_E10A1" V="26" />
<E C="P_E10E1" O="13_1" W='True'>
<A C="P_E10E1A1" V="25_1" />
<A C="P_E10E1A1" V="26_1" />
<E C="P_E10E1E1" O="13_1_1" W='True'>
<A C="P_E10E1E1A1" V="25_1_1" />
<A C="P_E10E1E1A1" V="26_1_1" />
</E>
</E>
</E>
<E C="P_E11" O="13" >
<A C="P_E11A1" V="25" />
<A C="P_E11A1" V="26" />
<E C="P_E11E1" O="13_1" W='True'>
<A C="P_E11E1A1" V="25_1" />
<A C="P_E11E1A1" V="26_1" />
<E C="P_E11E1E1" O="13_1_1" W='True'>
<A C="P_E11E1E1A1" V="25_1_1" />
<A C="P_E11E1E1A1" V="26_1_1" />
</E>
</E>
</E>
<E C="P_E12" O="14" >
<A C="P_E12A1" V="27" />
<E C="P_E12E1" O="15" W='True'>
<A C="P_E12E1A1" V="27_1" />
</E>
</E>
</P>

High-level Requirements:
Basically, I need to get all of the nodes where the attribute
W='True". These nodes need to be grouped together with the parent
element tag, plus attributes, that does not have a W attribute.

More Detail:
I need to merge this information into an existing system.
The W="True" indicates that the Entity (E) is a new element.
If an Entity (E) has an attribute of W="True" all child Entities
(E) will also have a W="True". (If the parent is new, all children,
grandchildren and etc are new as well.)
I need the parent element to reference the exiting item to merge in all
of the new Entities.

Here is what I have so far:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" />
<xsl:template match="/">
<root>
<!-- Get IsNew Entities Plus Parent-->
<IsNewPlusParent>

<!-- Get all entities which are new plus thier parents -->
<xsl:apply-templates select="//P [E[@W='True']] | //E
[E[@W='True']]" mode="IsNewPlusParent"/>

</IsNewPlusParent>

</root>
</xsl:template>
<!-- IsNew Plus Parent Template(s) - Start-->

<xsl:template match="P|E" mode="IsNewPlusParent">

<!-- Print P or E (parent of new entities element-->
<xsl:element name="{local-name()}">

<!-- Add all attributes to the element-->
<xsl:for-each select="@*">
<xsl:copy-of select="."/>
</xsl:for-each>

<!-- Add all E nodes that are new-->
<xsl:for-each select="child::E [@W='True']">
<xsl:copy-of select="."/>
</xsl:for-each>

</xsl:element>
</xsl:template>

<!-- IsNew Plus Parent Template(s) - End-->
</xsl:stylesheet>

The problem with the above code is that it processes not only the
existing parent but all of parents.

Here is the output I would like:
<root>
<IsNewPlusParent>
<P C="P" O="1">
<E C="P_E1" O="1" W="True">
<A C="P_E1A1" V="1" />
<A C="P_E1A1" V="2" />
</E>
<E C="P_E10" O="13" W="True">
<A C="P_E10A1" V="25" />
<A C="P_E10A1" V="26" />
<E C="P_E10E1" O="13_1" W="True">
<A C="P_E10E1A1" V="25_1" />
<A C="P_E10E1A1" V="26_1" />
<E C="P_E10E1E1" O="13_1_1" W="True">
<A C="P_E10E1E1A1" V="25_1_1" />
<A C="P_E10E1E1A1" V="26_1_1" />
</E>
</E>
</E>
</P>
<E C="P_E11" O="13">
<E C="P_E11E1" O="13_1" W="True">
<A C="P_E11E1A1" V="25_1" />
<A C="P_E11E1A1" V="26_1" />
<E C="P_E11E1E1" O="13_1_1" W="True">
<A C="P_E11E1E1A1" V="25_1_1" />
<A C="P_E11E1E1A1" V="26_1_1" />
</E>
</E>
</E>
<E C="P_E12" O="14">
<E C="P_E12E1" O="15" W="True">
<A C="P_E12E1A1" V="27_1" />
</E>
</E>
</IsNewPlusParent>
</root>

Again, thank you reading this. Any help would be appreciated.

Dec 8 '06 #1
1 1365
You said:
"the nodes where the attribute
W='True". These nodes need to be grouped together with the parent
element tag, plus attributes, that does not have a W attribute."

I'd suggest searching for the parent and processing downward from there.
Your parents are
//*[not @W][*/@W]
.... that is, all nodes which do not have a W attribute but which have a
child that does have this attribute.

Having found the interesting parents, it appears you want to flatten
their subtrees. (I don't understand why, but...) As you've already
discovered, that can be done by simply recursing down their trees and
copying the nodes you're interested in. However, I would suggest that
you consider using xsl:copy rather than explicitly constructing a new
element and copying the attributes to it; that'll make your life
significantly easier, especially when you start dealing with namespaces.

Hope that helps.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Patrick Guio | last post by:
Dear all, I am trying to use the std::transform algorithm to to the following vector< vector<char> >::iterator ik = keys.begin(); // key list iterator vector< vector<char> >::iterator is = ik;...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
8
by: Luther Miller | last post by:
I am using the XML tranform functionality in .NET to transform data in a DataSet into XMLSS using an XSLT file I have created. There are about 100 columns and only about 120 rows in the data...
3
by: Rouven Hertenstein | last post by:
Hi, I'm trying to test the vb.net-exercise "How to use a DataSet with the Office XP Chart Component and ASP.NET" on http://support.microsoft.com/kb/303016/en-us Visual Studio rails against the...
6
by: tcdevelopment | last post by:
I have a XSLT file that gives expected results when I transform using MSXML V4.0 in a simple vbs file. However when using XslTransform in dotnet, I do not get the same results. The part of the...
1
by: Danny Lesnik | last post by:
Hi i have my XML file c:\prd.xm <?xml-stylesheet type="text/xsl" href="prd.xsl"?><products><product><a>2</a><b>3</b></product><product><a>4</a><b>2</b></product></products This is my XSL file...
3
by: vitaly.tomilov | last post by:
I'm using an ASP.NET form to display data from my database table, and I'm doing it in the following way: XmlDataDocument doc = new XmlDataDocument(mydataSet); XPathNavigator nav =...
8
by: Dilip | last post by:
I was reading the C++ templates (Josuttis et al) book and came across an example that left me scratching my head.. there is a function template: template<typename T, int val> T addvalue(T...
4
by: Dean Card | last post by:
Okay, so here is the situation. I have need to do some on-the-fly image creation. I have everything working great except for the last part of it, applying a perspective type transform to the...
8
by: Alex Buell | last post by:
I've just written the below as an exercise (don't worry about the lack of checking), but I was wondering why I needed to write a struct with an operator() as a parameter to supply to the STL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.