|
Hello. How can I use XSL to get the most common value of a given attribute in some unsorted XML? For instance, here is some XML:
<fixtures>
<fixture time="1930" date="20080926"></fixture>
<fixture time="0930" date="20080927"></fixture>
<fixture time="0930" date="20080927"></fixture>
<fixture time="1000" date="20080928"></fixture>
<fixture time="1000" date="20080926"></fixture>
<fixture time="0930" date="20080926"></fixture>
<fixture time="1230" date="20080930"></fixture>
</fixtures>
I would like to end up with <xsl:value-of select="$mostCommonTime" xmlns:xsl="#unknown"></xsl:value-of>, therefore producing an output of "0930" - the most common value of the time attribute.
Just to complicate things, I need to do further validation on each fixture (using the date attribute), to see whether it is counted or not. This validation is done using Javascript, since it involves date functions, and we are using (and due to 3rd party restrictions, can only use) XSL 1.0. So, each fixture needs to be examined in a separate template, rather like this: <xsl:value-of select="lxslt:isFixtureValid(string(Fixture/@date))" xmlns:xsl="#unknown"></xsl:value-of>, and is discounted or counted depending on whether this output is 'true' or 'false'.
I've tried a recursive template, but do not see a way of sorting the Fixtures node that has to be passed into it; if I try a for-each loop, there is no way of keeping track of the most common time.
Thanks.
|
|
|
|
|
This does not seem like an appropriate use of XSLT. It is a data transformation mechanism not a data analysis mechanism. Therefore even if you could figure out how to do it, it doesn't mean it's a good idea.
led mike
|
|
|
|
|
This is part of a data transformation. I want to place an element around the fixtures, stating "time is hh:mm unless stated", where hh:mm is the most common value. The purpose of the stylesheet is certainly not analysis.
I think that this is a grouping problem, requiring Muenchian grouping, but I've got no further than that.
|
|
|
|
|
darasd wrote: This is part of a data transformation.
That's the way you are looking and perhaps implementing it but that is not what I am referring to.
darasd wrote: to get the most common value
That is an analysis activity, not a transformation activity. Do you understand my point?
led mike
|
|
|
|
|
I'm afraid I'm not convinced that because it is analysis, then it has no place in XSL. You could argue that any transformation is, when it comes down to it, an analysis activity. Why is any transformation performed? To allow the data to be output and therefore interpreted in a different way, which is simply another way of saying Analysis.
For example, a common activity is to list all the elements with a given attribute value. This in itself is a form of analysis.
|
|
|
|
|
|
How about something like this?
<xsl:variable name="mostCommonTime">
<xsl:for-each select="//fixtures/fixture">
<xsl:sort select="count(time)" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="count(time)"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
modified on Wednesday, October 15, 2008 11:31 PM
|
|
|
|
|
Sorry to make my first post something like this, but i'm having one of those 'bang your head against the wall' problems, I usually work with t-sql and only use xml for outputs but today I have to parse a list in c# and output to html. I have had a search but I think as the xml i've been given is badly formed I've had little luck searching.
Anyway....
XML:
<data>
<person>
<name>peter jakes</name>
<path>pj87</path>
<property name="System">Linux</property>
<property name="Distro ID">Redhat</property>
</person>
</emucenter>
</data>
All, I want to do in xsl is display the text 'Linux' and 'Redhat' by using a name reference..
I can get this text if I use an ordinal number like so...
<xsl:value-of select="data/person/property[1]"/>
...this will display 'Linux', great. But I need to use the actual name reference not a number.
|
|
|
|
|
Once you have well formed XML rather than what you posted this XPath statement will return the property node in question
//data/person/property[@name='System']
A good reference for things like XPath is www.w3schools.com
good luck
led mike
|
|
|
|
|
thanks, I actually got there in the end, I did try w3schools first actually but they didn't have an example that fit how I wanted.
|
|
|
|
|
Consider this function:
BOOL GetSingleNodeV(const MSXML2::IXMLDOMDocumentPtr pXMLDoc, const CString &strPath, CString &StrValue)
{
BOOL bRetCode=FALSE;
BSTR bstrQstr = strPath.AllocSysString();
IXMLDOMNodePtr o;
o = pXMLDoc->selectSingleNode( bstrQstr);
if (o!=NULL)
{
HRESULT hr=o->get_text(&bstrQstr);
if (hr==S_OK)
{
StrValue = bstrQstr;
bRetCode = TRUE;
}
else
{
StrValue="";
}
}
//o->Release();
SysFreeString(bstrQstr);
return bRetCode;
}
Call to selectSingleNode() sometimes works as expected but sometimes it throws an exception. How can I extract additional information about this exception?
modified on Tuesday, September 30, 2008 1:52 PM
|
|
|
|
|
#import generated ATL code throws _com_error exceptions
When all else fails, consult the documentation[^]
led mike
|
|
|
|
|
Is possible to create an XmlDataSource that has 50 columns merge an sql database with 14 columns into and force the other 36 columns except and false or any value I want with c# or vb
Learning to Code
|
|
|
|
|
Ok. Pick a forum and stick with it. Posting the same question all over the site is considered rude.
|
|
|
|
|
Sorry, Just looking for help I am new.
Learning to Code
|
|
|
|
|
Hi All,
I have this XML files here:
<?xml version="1.0" encoding="utf-8">
<?xml-stylesheet type="text/xsl" href="TimesheetSQLScript.xsl"???>
<myfields>
<timesheetid>
<companycode>CCCRM</companycode>
<employee>Adrian</employee>
<weekpick>2008-09-15</weekpick>
<startdate>2008-09-15</startdate>
<note></note>
<enddate>2008-09-21</enddate>
<monday>
<mondate>2008-09-15</mondate>
<moncheck>
<moncheckin>14:10:00</moncheckin>
<moncheckout>15:44:00</moncheckout>
</moncheck>
<moncheck>
<moncheckin>16:44:00</moncheckin>
<moncheckout>22:50:00</moncheckout>
</moncheck>
</monday>
</timesheetid>
</myfields>
And I applied this XSL:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="/">
Declare @TimeCardID int
<xsl:apply-templates select="myFields/TimesheetID/Monday" />
</xsl:template>
<xsl:template match="Monday">
<xsl:text>set @TimeCardID = exec spCreateTimeCard '</xsl:text>
<xsl:value-of select="MonDate" />
<xsl:text>', '</xsl:text>
<xsl:value-of select="/myFields/TimesheetID/Employee" />
<xsl:text>'</xsl:text>
<xsl:for-each select="MonCheck">
exec spCreateTimeCardSession @TimeCardID, '
<xsl:value-of select="MonCheckIn" />
<xsl:text>', '</xsl:text>
<xsl:value-of select="MonCheckOut" />
<xsl:text>'</xsl:text>
</xsl:for-each>
</xsl:template>
It generate this sql statement:
Declare @TimeCardID int
set @TimeCardID = exec spCreateTimeCard '2008-09-15', 'Adrian'
exec spCreateTimeCardSession @TimeCardID, '
14:10:00', '15:44:00'
exec spCreateTimeCardSession @TimeCardID,
'16:44:00', '22:50:00'
but this is not what i want, is there a way that i can generate this statement like this:
Declare @TimeCardID int
set @TimeCardID = exec spCreateTimeCard '2008-09-15', 'Adrian'
exec spCreateTimeCardSession @TimeCardID, '14:10:00', '15:44:00'
exec spCreateTimeCardSession @TimeCardID, '16:44:00', '22:50:00'
Thanks in advance </xsl:stylesheet>
|
|
|
|
|
don't worry folks, i fixed by myself. just have to put <xsl:text></xsl:text> tags like this:
exec spCreateTimeCardSession @TimeCardID, '<xsl:text></xsl:text>
|
|
|
|
|
How can I create a schema for the following?
<Data>
<Value unit="m/s">10</Value>
<Value unit="ºF">25</Value>
</Data>
I would like to have the following description in the xsd
<xs:complexType name="rootSchema">
<xs:sequence>
<xs:element name="Value" type="valueSchema" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:element name="Multicalc" type="rootSchema" />
How do I write the definition of valueSchema?
(I hope I didn't make any mistakes in the definitions. I simplified things a bit compared to my own xsd.)
|
|
|
|
|
see if this works
<xs:complexType name="valueSchema">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="unit" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
led mike
|
|
|
|
|
Thanks a bunch. It did the trick it seems.
|
|
|
|
|
I want to add rss feed into the html page, but I have a problem. When I displayed the xml file with xsl. It only works at IE, but doesn't work at Firefox.
Tell me why? Thanks.
|
|
|
|
|
josseph.zhang wrote: When I displayed the xml file with xsl. It only works at IE, but doesn't work at Firefox.
Tell me why?
You need to explain this in more detail:
josseph.zhang wrote: I displayed the xml file with xsl
led mike
|
|
|
|
|
I am assuming you are using the Style Sheet Processing Instruction,
<?xml-stylesheet type="text/xsl" href="filename.xslt"?><br /> . Not all browsers (especially their older versions) are able to transform XML using this Processing Instruction. However, the following link may be helpful: http://www.ibm.com/developerworks/web/library/x-think40/[^].
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Yes, you are clever.I used it.The link given is very useful to me. Thanks a lot.
|
|
|
|
|
All
I have a web service that has been working fine until yesterday evening, I don't remember making any changes, but for some reason since yesterday evening I am getting the message "WSE910: An error happened during the processing of a response message, and you can find the error in the inner exception. You can also find the response message in the Response property." with inner exception as "Root Element Missing". I checked the xml file generated and can't find anything mistakes in it. I am using the same machine as client/server so there is no point of time difference.
Appreciate your help
Sri
|
|
|
|