-1

Say I have some XML like this:

<xml>
<Burger>
  <CookTime>"2023-04-03T15:13:33.8100000"</CookTime>
  <User>Billy</User>
</Burger>
<Burger>
  <CookTime>"2023-04-01T15:13:33.8100000"</CookTime>
  <User>Bob</User>
</Burger>
<Burger>
  <CookTime>"2023-04-03T17:13:33.8100000"</CookTime>
  <User>Jill</User>
</Burger>
</xml>

I'm trying to work with an XSLT (in 1.0, can't help that) that iterates through this. What I need to be able to do is select the node that has the LATEST date time stamp, and put THAT User belonging to that node inside a NEW node later in the xml:

<LatestBurgerCook>
   Jill
<LatestBurgerCook>

I thought, hey, maybe I'll try to pass an array of this into a C# function and do it there! I have some C# in this XSLT in the relevant code section, but I don't know how to pass multiple path elements into it. If I pass this XPath in:

<xsl:variable name="maxCookedTime">
   <xsl:value-of select="userCSharp:LoadLatestCookTime(xml/Burger/CookTime)"/>
</xsl:variable name="maxCookedTime">

I'm thinking, oh hey, I can maybe use that C# function to go through each of these elements? But I am not sure what that C# function would look like. I've seen examples using XPathIterator, but that seems to be for a single XML object? I need to be able to send multiple matches of this cook time to my function.

Does anybody know if this can be done?

Here's the C# function I tried to use, but to no avail:

public static string LoadLatestCookTime(XPathNodeIterator nodes)
{
    List<DateTime> dateTimes = new List<DateTime>();
        
    foreach(var node in nodes)
        dateTimes.Add(Convert.ToDateTime(s));
            
    DateTime maximumDateTime = dateTimes.Max();
            
    return maximumDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffff");
}

If I'm going about this the wrong way, I'm open to hearing a different way to do this, too. But keep in mind - I'm limited to XSLT 1.0 with MSXSL C# functions.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kieran Ojakangas
  • 495
  • 4
  • 18

1 Answers1

0

you can try this code to get max date

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);

    string latestCook = LoadLatestCook(xmlDoc);

    XmlElement elem = xmlDoc.CreateElement("LatestBurgerCook");
    elem.InnerText = latestCook;

    xmlDoc.LastChild.AppendChild(elem);

public static string LoadLatestCook(XmlDocument xmlDoc)
{

JArray users = (JArray)JObject.Parse(JsonConvert.SerializeXmlNode(xmlDoc,
                            Newtonsoft.Json.Formatting.None, true))["Burger"];

return (string)users.MaxBy(u => DateTime.Parse(
                               (string)JToken.Parse((string)u["CookTime"])))["User"];
}

output

<xml>
   <Burger>
      <CookTime>"2023-04-03T15:13:33.8100000"</CookTime>
      <User>Billy</User>
   </Burger>
   <Burger>
      <CookTime>"2023-04-01T15:13:33.8100000"</CookTime>
      <User>Bob</User>
   </Burger>
   <Burger>
      <CookTime>"2023-04-03T17:13:33.8100000"</CookTime>
      <User>Jill</User>
   </Burger>
   <LatestBurgerCook>Jill</LatestBurgerCook>
</xml>
Serge
  • 40,935
  • 4
  • 18
  • 45