Note that there are some explanatory texts on larger screens.

plurals
  1. POXSLT 1.0: Sorting by concating portions of date string
    primarykey
    data
    text
    <p>I'm trying to take XML data and sort elements by their data attribute. Unfortunately the dates come over in mm/dd/yyyy format and are not static lengths. (Jan = 1 instead of 01) So I believe the string will have to be parsed into three components and the month padded. The newly concated value (yyyymmdd) then sorted descending.</p> <p>Problem is I have no idea how to do this. Here is an example of the data</p> <pre><code>&lt;content date="1/13/2011 1:21:00 PM"&gt; &lt;collection vo="promotion"&gt; &lt;data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM" type="base"/&gt; &lt;data vo="promotion" promotionid="64646" code="101P026" startdate="2/19/2011 12:00:00 AM" type=""/&gt; &lt;data vo="promotion" promotionid="64636" code="101P046" startdate="1/9/2011 12:00:00 AM" type="base"/&gt; &lt;/collection&gt; &lt;/content&gt; </code></pre> <p>Also can anyone please recommend a good book on learning XSLT?</p> <p>Thanks!</p> <h2><strong>Update 1</strong></h2> <p>I really wish I had a better understanding of this LOL Anyways, I used the code you provided and added the 'value-of' code that worked in the related code you provide in another question and am seeing no results. Ideally once this has been sorted I would then need to reference multiple other attributes from the most recent data element.</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="node()|@*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/"&gt; &lt;xsl:variable name="vrtfPass1"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:variable&gt; &lt;xsl:apply-templates mode="pass2" select= "ext:node-set($vrtfPass1)/*"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@startdate"&gt; &lt;xsl:variable name="vDate" select="substring-before(.,' ')"/&gt; &lt;xsl:variable name="vYear" select= "substring($vDate, string-length($vDate) -3)"/&gt; &lt;xsl:variable name="vDayMonth" select= "substring-before($vDate, concat('/',$vYear))"/&gt; &lt;xsl:variable name="vMonth" select="format-number(substring-before($vDayMonth, '/'), '00')"/&gt; &lt;xsl:variable name="vDay" select="format-number(substring-after($vDayMonth, '/'), '00')"/&gt; &lt;xsl:attribute name="startdate"&gt; &lt;xsl:value-of select="concat($vYear,$vMonth,$vDay)"/&gt; &lt;/xsl:attribute&gt; &lt;/xsl:template&gt; &lt;xsl:template match="node()|@*" mode="pass2"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates mode="pass2" select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template mode="pass2" match="collection"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates mode="pass2" select="@*"/&gt; &lt;xsl:apply-templates mode="pass2"&gt; &lt;xsl:sort select="@startdate"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="content/collection/data"&gt; &lt;xsl:if test="position()=1"&gt; &lt;xsl:value-of select="@promotionid"/&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <h2><strong>Update 2</strong></h2> <p>Hmm, well I tried updating it like you said</p> <pre><code>... &lt;/xsl:template&gt; &lt;xsl:template mode="pass2" match="content/collection/data"&gt; &lt;xsl:value-of select="@promotionid"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>And I still don't get any output. I googled around a bit and also tried messing with this declaration <code>xmlns:ext="http://exslt.org/common"</code> and tried different values based on an article I looked at. I tried</p> <ul> <li><a href="http://exslt.org/common" rel="nofollow">http://exslt.org/common</a></li> <li>urn:schemas-microsoft-com:xslt</li> <li><a href="http://xml.apache.org/xalan" rel="nofollow">http://xml.apache.org/xalan</a> (changed the node-set to nodeset)</li> </ul> <p>And nothing provided output. So I wonder if I have something wrong or if my xslt processor doesn't support it.</p> <h2><strong>Update 3</strong></h2> <p>Okay apparently we've been given, repeatedly, bad information. I've update the sample XML with another attribute which changes what needs to be done. </p> <p>What needs to happen is the data be sorted by date like we've already done and then pull the promotionid of the data node that is the most recent AND has the type='base'. If no data node has type='base' than we just reference the most recent data node like we've already have working.</p> <p>Hope that makes sense. And once again thanks so much.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload