473,772 Members | 3,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT to "normalize" weight attribute

Hi,

I've been knocking my head against the wall trying to create an
XSL transform to perform "normalizations " of a set of XML files
that have a common structure.
% XML file before transform
<base>
<foo>
<bar weight="20">
<elementOne>asf d</elementOne>
<elementTwo>qwe r</elementTwo>
</bar>
<bar weight="5">
<elementOne>asf d</elementOne>
<elementTwo>qwe r</elementTwo>
</bar>
<bar weight="30">
<elementOne>asf d</elementOne>
<elementTwo>qwe r</elementTwo>
</bar>
</foo>
</base>
% XML file after transform
<base>
<foo weightSum="55">
<bar weight="20" lower="1" upper="20">
<elementOne>asf d</elementOne>
<elementTwo>qwe r</elementTwo>
</bar>
<bar weight="5" lower="21" upper="25">
<elementOne>asf d</elementOne>
<elementTwo>qwe r</elementTwo>
</bar>
<bar weight="30" lower="26" upper="55">
<elementOne>asf d</elementOne>
<elementTwo>qwe r</elementTwo>
</bar>
</foo>
</base>

The idea is that a random number between 1 and weightSum would be
selected, and then the child element of the element w/ the weightSum
attribute that has lower<=randomNu m<=upper would be selected. This
is a transformation that would only be run when the underlying xml
files
have been updated, so speed of transformation is not an issue.

Constraints: I have many files with this 'weightSum'-'weight' pattern,
and the element names ('foo' and 'bar' in the example above) differ
from file to file. Furthermore, it would be great if the transform
worked on nested 'weightSum'-'weight' patterns, such as the following.
% XML file before transform
<base>
<foo>
<bar weight="20">
<elementOne weight="3">asfd </elementOne>
<elementTwo weight="8">qwer </elementTwo>
</bar>
...
</foo>
</base>
% XML file after transform
<base>
<foo weightSum="55">
<bar weight="20" lower="1" upper="20" weightSum="11">
<elementOne weight="3" lower="1" upper="3">asfd</elementOne>
<elementTwo weight="8" lower="4" upper="11">qwer </elementTwo>
</bar>
...
</foo>
</base>
Any help would be appreciated.
- Arnold

Mar 2 '06 #1
1 2040
Here is a solution my earlier post. I used the Saxon8.7b parser.
I don't know if the solution relies on any XSLT 2.0 capabilities,
I need to test it with a XSLT 1.0 compliant parser.

The setup is as follows: A parent "container" element holds a
number of children elements with the same tag name. You want to
make it easy for a program to randomly select a child element with
a frequency that varies for each child. So in the example 'XML
input file' below, the first parent "container" element is named
'people' and thre are three children with the tag name 'person'.
The weights for the three children are '80, '10' and '40'. So
80/(80+10+40)% of the time I want to select the first 'person'
element. Likewise, within the first person element, I want to
select the first 'given' element 35/(35+25+10)% of the time.

Notes:
- The solution seems to work on nested weightSum-weight
combinations.
- For reasons I don't understand, simply applying the
transformation to the XML input file results in extra blank
lines. I use awk in a shell script to get rid of the blank
lines.
- Referring to the 'XML output file', a program would randomly
select (say) a 'person' by
1- reading the value of the 'weightSum'attr ibute for the
parent element 'persons'
2- randomly drawing between 0 and weightSum-1
3- locating the 'person' element s.t. the random number is
= the 'lower' attribute value and < the 'upper'

attribute value.


%------------------- XML input file ------------------------------
<?xml version="1.0"?>
<people weightSum="100" >
<person weight="80">
<givens weightSum="0">
<given weight="35">Alf red</given>
<given weight="25">Fre d</given>
<given weight="10">Wil fred</given>
</givens>
<family>Newma n</family>
</person>
<person weight="10">
<givens>
<given>Leslie </given>
</givens>
<family>Newma n</family>
</person>
<person weight="40">
<givens>
<given>Maria</given>
</givens>
<family>Newma n</family>
</person>
</people>
%------------------- XML output file -----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<people weightSum="130" >
<person weight="80" lower="0" upper="80">
<givens weightSum="70">
<given weight="35" lower="0" upper="35">Alfr ed</given>
<given weight="25" lower="35" upper="60">Fred </given>
<given weight="10" lower="60" upper="70">Wilf red</given>
</givens>
<family>Newma n</family>
</person>
<person weight="10" lower="80" upper="90">
<givens>
<given>Leslie </given>
</givens>
<family>Newma n</family>
</person>
<person weight="40" lower="90" upper="130">
<givens>
<given>Maria</given>
</givens>
<family>Newma n</family>
</person>
</people>


%------------------- XSLT file -----------------------------------
<?xml version="1.0"?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- The xsl:choose statement is used have this default template
match -->
<!-- everything EXCEPT elements with a 'weight' attribute.
-->
<xsl:template match="@*|node( )">
<xsl:choose>
<xsl:when test="@weight"> </xsl:when>
<xsl:otherwis e>
<xsl:copy>
<xsl:apply-templates select="@*|node ()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- Here we match the element nodes that have a 'weight' attribute
-->
<xsl:template match="attribut e::weightSum">
<xsl:attribut e name="weightSum ">
<xsl:value-of select="sum(../child::*/attribute::weig ht)" />
</xsl:attribute>

<xsl:for-each select="../child::*">
<xsl:variable name="weight" select="attribu te::weight" />
<xsl:variable name="from"
select="sum(./preceding-sibling::*/attribute::weig ht)" />
<xsl:variable name="to"
select="sum(./preceding-sibling::*/attribute::weig ht)+$weight" />

<xsl:copy>
<xsl:attribut e name="weight" >
<xsl:value-of select="$weight " />
</xsl:attribute>
<xsl:attribut e name="lower">
<xsl:value-of select="$from" />
</xsl:attribute>
<xsl:attribut e name="upper">
<xsl:value-of select="$to" />
</xsl:attribute>
<xsl:apply-templates select="@*|node ()"/>
</xsl:copy>
</xsl:for-each>

</xsl:template>

</xsl:stylesheet>
%------------- Script to remove extra blank lines ----------------
#!/bin/bash

argc="$#"

if [ \( "$argc" -lt 1 \) -o \( "$argc" -gt 2 \) ]; then
printf "\n\n"
printf " Usage: NormalizeWeight s.sh data.xml [output_file]"
printf "\n\n"
exit 1
fi

if [ "$argc" -eq 1 ]; then
inputXmlFname=$ 1;
/usr/bin/java -jar $HOME/sbox/software/lib/saxon8.7/saxon8.jar -t
$inputXmlFname NormalizeWeight s.xsl | /usr/bin/awk '!/^( )+$/{print
$0;}'
elif [ "$argc" -eq 2 ]; then
inputXmlFname=$ 1;
outputXmlFname= $2;
if [ -f "$outputXmlFnam e" ]; then
backupName=$(pr intf "%s%s" $outputXmlFname ".bac" )
echo "File $outputXmlFname exists, making backup named
$backupName"
/bin/cp $outputXmlFname $backupName
fi
/usr/bin/java -jar $HOME/sbox/software/lib/saxon8.7/saxon8.jar -t
-o $outputXmlFname $inputXmlFname NormalizeWeight s.xsl
/bin/cat $outputXmlFname | /usr/bin/awk '!/^( )+$/{print $0;}' >
tmp$$
/bin/mv tmp$$ $outputXmlFname
/bin/rm -f tmp$$
fi

Mar 5 '06 #2

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

Similar topics

9
4256
by: Christian Roth | last post by:
Hello, when using this "identity" processing sheet: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="iso-8859-1" /> <xsl:template match="@*|node()">
2
1140
by: David Blickstein | last post by:
My XSLT stylesheet has the following top-level parmeter definition: <xsl:param name="MethodName">&apos;Foobar&apos;</xsl:param> Later in the stylesheet I try to base an xsl:if test on it: <xsl:if test="normalize-space( @Method ) = normalize-space($MethodName)"> The test condition is never satisfied. Now if I substitute the value of the parameter in:
7
2155
by: Scott W Gifford | last post by:
Hello, I'm considering using XML to represent a stream of location information, and XPath to do queries against it. I've got most of it figured out (at least on paper), but I can't figure out how to create an XPath statement asking for the "last node with a value less than" a given value. I need this to be able to ask "Where was Scott at 11:00 yesterday", which should find the last sighting of Scott before or at 11:00
4
1929
by: Nathan Benefield | last post by:
I currently have a spreadsheet tracking votes on legislation in a matrix type format. It is something like this Name Act1 Veto1 Act1A Jones yes No Yes Johnson Yes Yes Ex. Only with many more members and bills. I want to normalize this so that i can create reports by both Bill and by member - linking it to tables
4
1991
by: Mario Vázquez | last post by:
Hi, I'm trying to put the links (href attribute of the <a> element) out of my XSLT files, in a XML file, and read it from the stylesheet. I suppose that I have to use the document() function to link to the XML file containing the URLs, but I don't know how to do it. I've tried something like this: This is XML file containing the URLs (link.xml): <?xml version="1.0" encoding="utf-8" ?> <LINKS>
6
2315
by: AndyL | last post by:
Hi, I have a lot of sources with mixed indentation typically 2 or 4 or 8 spaces. Is there any way to automatically convert them in let's say 4 spaces? Thx, A.
13
2319
by: jtric | last post by:
Very new to XML/XSL, so please forgive me if this is an incredibly simple question. I've been pulling my hair out over this for several days now. I'm working on an internal application that takes the output from a custom Excel spreadsheet, and creates a Final Cut Pro sequence in XML. Due to the way Excel handles nested repeating elements, I need to find a way to replace a dummy element I have created with a whole series of repeating elements....
11
2091
by: =?ISO-8859-1?Q?Jean=2DFran=E7ois_Michaud?= | last post by:
Context: I'm trying to compare XML tree fragments and I'm doing so by outputting the attributes of each element in the tree and outputting it to a string then normalizing the strings. Then I'm doing a contains of the current string against the following-sibling::* to determine if we have duplicates. If we have a duplicate, we move to the next item, if there is no duplicate, we output the small tree. I'm hitting a completely ridiculous...
0
9620
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.