XSLT number only counts instances in current file of a multi-file document - xslt

I've been tasked with putting together a book (using XSL FO) from a number of XML-files, now I'm trying to number the figures in this book (simple numbering, no resets at chapters or whatever), my naive approach was to do this
<xsl:template match="figure">
<fo:block xsl:use-attribute-sets="figure">
.. stuff to deal with images ..
<fo:block xsl:use-attribute-sets="figure-caption">
Figure <xsl:number level="any"/>: <xsl:apply-templates/>
</fo:block>
<fo:block xsl:use-attribute-sets="figure-caption">
</xsl:template>
I have an aggregate XML file which selects the files to use using the document() function like so:
<xsl:template match="include">
<xsl:apply-templates select="document(#src)"/>
</xsl:template>
Now, my problem is that number seems to always only count the instances in the current file, which is not what I want (currently, there's only one or two images per file, resulting in all figures being 'Figure 1' or 'Figure 2').
I've considered two approaches, both being essentially two-pass XSLT. First, the straightforward approach, generate an intermediary XML containing the entire book using an identity transform, which I'm reluctant to do for other reasons.
Second, using node-set() extension, which I tried like this
<xsl:template match="include">
<xsl:apply-templates select="ext:node-set(document(#src))"/>
</xsl:template>
but this produced the same result.
Any ideas? Perhaps something which isn't a two-pass transformation? Any help would be greatly appreciated.

The two -pass approach is the more logical and robust one.
One-pass approach is very challenging. One can provide an expression in the value attribute of <xsl:number> and this can be used to sum the "local number" with the maximum accumulated number so far from all previous documents.
However, this requires sequencing the documents (which is something bad in a functional language) and this only works for a flat numbering scheme. In case hierarchical numbering is used (3.4.2), I don't see an easy way to continue from the max number of a previous document.
Due to this considerations, I would definitely merge all documents into one before numbering.

I will also use a two phase transformation. But just for fun, with one include level and no repetition, this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="vIncludes" select="//include"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="include">
<xsl:apply-templates select="document(#src)"/>
</xsl:template>
<xsl:template match="picture">
<xsl:variable name="vRoot" select="generate-id(/)"/>
<xsl:variable name="vInclude"
select="$vIncludes[
$vRoot = generate-id(document(#src))
]"/>
<xsl:copy>
<xsl:value-of
select="count(
document(
(.|$vInclude)/preceding::include/#src
)//picture |
(.|$vInclude)/preceding::picture
) + 1"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With this input:
<master>
<include src="child3.xml"/>
<block>
<include src="child1.xml"/>
<picture/>
</block>
<include src="child2.xml"/>
<picture/>
</master>
And 'child1.xml'
<child1>
<picture/>
</child1>
And 'child2.xml'
<child2>
<picture/>
</child2>
And 'child3.xml'
<child3>
<picture/>
</child3>
Output:
<master>
<child3>
<picture>1</picture>
</child3>
<block>
<child1>
<picture>2</picture>
</child1>
<picture>3</picture>
</block>
<child2>
<picture>4</picture>
</child2>
<picture>5</picture>
</master>

You could use an ancillary XML documentto keep track of the last figure number and load that file as a document from your stylesheet. Or, if you do not want to manage two output files from the same stylesheet (the "real" FOP output and the figure counter) you can simply load the previous chapter's FOP file and look for the MAX of the figure-caption.
Or you could pass the last figure number as a parameter with default zero and pass the parameter on the command line. The value of this parameter resulting from the parsing of the previous one in the ascending resulting document order.
All these alternatives suppose you are running the transformations in sequence in source document ascending order.
A more structured and robust solution would be to manage transverse document sections such as indices, table of contents and table of figures in as many separate FO documents that would be generated in a "second pass" run with their own XSLT.

I think I would do a prepass which outputs summary information about all the documents in a single XML file, and then use this as a secondary input to the number calculation. The summary information in your case might just be a count of how many figures each document contains, but in many cases it can be useful to hold other information as well such as the IDs of sections that will act as the target of hyperlinks.

Related

xsl 3.0: How to process certain child elements first in xsl:apply-templates, then the remainder (overriding document order)

Assume my xml input is a MFMATR element with a few child elements, such as: TRLIST, INTRO, and SBLIST -- in that document order. I am converting to HTML.
I have a template that matches on the MFMATR element, and wants to run xsl:apply-templates on the 3 child elements, but I want INTRO to be processed first (listed first in the HTML). The other two (TRLIST and SBLIST) should keep their relative document order, as long as INTRO comes before both of them.
So I'd like to run <xsl:apply-templates select="INTRO, *"> but not have INTRO matched twice. (Using this syntax with xsl 3.0 causes dupes for me.) I also don't want to explicitly list every tag in the select expression, so unknown tags will still be processed.
A 2nd real life example is this: <xsl:apply-templates select="TITLE, CHGDESC, *"/>. Again, right now that is causing dupes I don't want.
I am using Saxon.
So I'd like to run <xsl:apply-templates select="INTRO, *"> but not have INTRO matched twice
Try:
<xsl:apply-templates select="INTRO, * except INTRO">
This seems to work. If someone has a better answer, let me know and I will change it.
There is no DRY violation here -- no repeated element names or variable names. I want it to look clean at all the call sites I will have.
It seems idiomatic to me since the function was pulled from w3's own website!
<xsl:template match="MFMATR">
<!-- Process INTRO first, no matter where it appears -->
<xsl:variable name="nodes" select="INTRO, *"/>
<xsl:apply-templates select="kp:distinct_nodes_stable($nodes)"/>
</xsl:template>
<xsl:template match="INTRO">
<xsl:variable name="nodes" select="TITLE, CHGDESC, *"/>
<xsl:apply-templates select="kp:distinct_nodes_stable($nodes)"/>
</xsl:template>
<!-- Discard duplicate elements in $seq, but keep their ordering -->
<!-- Adapted from https://www.w3.org/TR/xpath-functions/#func-distinct-nodes-stable -->
<xsl:function name="kp:distinct_nodes_stable" as="node()*">
<xsl:param name="seq" as="node()*"/>
<xsl:sequence select="fold-left($seq, (),
function($foundSoFar as node()*, $this as node()) as node()* {
if ($foundSoFar intersect $this)
then $foundSoFar
else ($foundSoFar, $this)
}) "/>
</xsl:function>

Is there a way to count the elements generated by an XSL within the same XSL?

If I have an XSL that creates output like this simple/rough example:
<Parent1>
<ABC><xsl:value-of select="SomeValue1"/></ABC>
<DEF><xsl:value-of select="SomeValue2"/></DEF>
<GHI><xsl:value-of select="SomeValue3"/></GHI>
...
<YZ><xsl:value-of select="SomeValue9"/></YZ>
</Parent1>
... within this same XSL, how can I count how many children the XSL will produce?
You can generate your content into a variable, count the children in the variable, and then emit the content of the variable:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:variable name="temp-results">
<Parent1>
<ABC><xsl:value-of select="SomeValue1"/></ABC>
<DEF><xsl:value-of select="SomeValue2"/></DEF>
<GHI><xsl:value-of select="SomeValue3"/></GHI>
...
<YZ><xsl:value-of select="SomeValue9"/></YZ>
</Parent1>
</xsl:variable>
<xsl:text>Number of children:</xsl:text>
<xsl:value-of select="count($temp-results/Parent1/*)"/>
<xsl:sequence select="$temp-results"/>
</xsl:template>
</xsl:stylesheet>
One possibility is wrapping the whole output process in a variable and then count its descendants.
So, for example, you can use the following XSLT code
<xsl:template match="/Parent1">
<xsl:variable name="output">
<ABC><xsl:value-of select="SomeValue1"/><ZZZ>Some Grandchild</ZZZ></ABC>
<DEF><xsl:value-of select="SomeValue2"/></DEF>
<GHI><xsl:value-of select="SomeValue3"/></GHI>
...
<YZ><xsl:value-of select="SomeValue9"/></YZ>
</xsl:variable>
<xsl:value-of select="concat('Outputting ', count($output/descendant::*), ' elements.
')" />
<xsl:copy-of select="$output" />
</xsl:template>
Its output is
Outputting 5 elements.
<ABC>
<ZZZ>Some Grandchild</ZZZ>
</ABC>
<DEF/>
<GHI/>
...
<YZ/>
This code accomplishes three things:
First it generates the result and puts it into the variable
It counts all the
children (child::* axis) or
descendants (descendant::* axis) as in the example above
of the elements in the variable
It copies the variable to the output stream
This approach can even be nested - meaning that it can be applied several times, one after another.
Your choices are:
(a) find a way of computing the result as a function of the input
(b) capture the output in a variable and run a second phase of processing against that variable.
(c) a blend of the above: compute some intermediate result in a variable, and use that variable as input to both processes.
In the example you've given, the first approach works perfectly well; but I guess your real problem is more complex than that, otherwise you wouldn't be asking.

Is there a way to add a straight line between two flow section in renderX (XSLT)

I'm using the rx:flow-section functionnaly of RenderX to separate a page in two. However I would like to have a straight line that would go all the way through the page between the two flow section.
Is this something possible ? I have a limited knowledge, and I assume it would not be possible to add a fo:leader because if I had one it will be duplicated on each side of the flow-section.
Well, RenderX XEP does not provide a special extension to draw the gutter/column rule. However, it can be done. one way that is likely not desired would be to format all other content with a background-color of white and insert a full-page length rule or set of rules.
I will give this solution. It will scare off most because it is only internally probably doing what you could do. But is also shows just a small sample of what you can do.
I have done this in the past by taking advantage of RenderX's Intermediate Output format (XEPOUT) and a few tricks. When you are using RenderX, you can request to output XEPOUT instead of the final output format (like PDF). XEPOUT is a structured and documented XML format. You can use XSL to modify it and then send that modified XEPOUT back to the engine to get the final PDF.
Essentially the process would be:
XML + XSL -> XEPOUT + XSL -> new XEPOUT -> RenderX -> PDF
Just adding that one step in the process to use an XSL to modify XEPOUT. I will post this below, if you need more information on how to make this work in your environment, it would highly depend on how you use or integrate RenderX.
There are many tricks one can implement. In this case, what I did is apply a red background color behind the rx:flow-section. If you formatted that to the XEPOUT you would find in the content (among all the other text and stuff):
<xep:rgb-color red="1.0" green="0.0" blue="0.0"/>
<xep:rectangle x-from="72000" y-from="93600" x-till="282000" y-till="676800"/>
This is the red rectangle behind each of the columns of the flow-section.
If I were to format that document I would get this:
But if I instead format to XEPOUT and then use XSL to process it, I can change the document before creating the PDF.
Using a simple XSL, I could actually remove those red rectangles and use the dimensions and make a line between the columns. This example assumed only a two column document but you could modify to be as you wish (including picking an alternate color than red). I didn't do the full job here, you could enhance this to more center the line, or even implement multiple lines. This is only an example to get you rolling should you choose to do something like this.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="http://exslt.org/math"
xmlns:xep="http://www.renderx.com/XEP/xep" exclude-result-prefixes="math" version="1.0">
<xsl:template match="xep:page">
<!-- Get page width -->
<xsl:variable name="page-width">
<xsl:value-of select="number(#width)"/>
</xsl:variable>
<!-- get lower and upper y-pos of longest line {color} xep:rectangle -->
<xsl:variable name="y-till-pos">
<xsl:value-of select="math:min(xep:rgb-color[#red='1.0']/following-sibling::xep:rectangle[1]/#y-till)"/>
</xsl:variable>
<xsl:variable name="y-from-pos">
<xsl:value-of select="xep:rgb-color[#red='1.0']/following-sibling::xep:rectangle[1]/#y-from - 12000"/>
</xsl:variable>
<xep:page>
<xsl:apply-templates select="#*"/>
<!-- Draw Line -->
<xep:line x-from="{$page-width div 2 - 500}" y-from="{$y-from-pos}" x-till="{$page-width div 2 + 500}" y-till="{$y-till-pos}" thickness="1000" style="solid"/>
<xsl:apply-templates select="*"/>
</xep:page>
</xsl:template>
<!-- remove red and rectangle -->
<xsl:template match="xep:rectangle[preceding-sibling::*[1][name()='xep:rgb-color'][#red='1.0']]"/>
<xsl:template match="xep:rgb-color[#red='1.0']"/>
<!-- identity copy rules -->
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The result in two-page view in PDF shows the result:
Which is a rx:flow-section with a column divider like you wish.
As I said, it is a lot but there is so much more you can do using the same techniques. Like bookfolding results or making n-up pages or applying gradients to text or injecting page count marks or barcodes.

Change text of elements identified by dynamic XPath

I have an XML with 2 XML fragments, 1st one is a fragment where the new values must be applied (which can have pretty complex elements) like
... some static parents
<a:element1>
<a:subelement tag="someString">
<a:s1>a</a:s1>
</a:subelement>
</a:element1>
<a:element2>b</a:element2>
<a:element3>c</a:element3>
... lots of other elements like the above ones
and 2nd fragment that has XPaths generated from the first XML and a new value, like
<field>
<xpath>/Parent/element1/subelement[#tag="someString"]/s1</xpath>
<newValue>1</newValue>
</field>
<field>
<xpath>/Parent/element2</xpath>
<newValue>2</newValue>
</field>
We might not have new values to apply for all the elements in the first fragment.
I'm struggling to make an XSLT transformation that should apply the new values to the places indicated by the XPaths.
The output should be:
... some static parents
<a:element1>
<a:subelement tag="someString">
<a:s1>1</a:s1>
</a:subelement>
</a:element1>
<a:element2>2</a:element2>
... lots of other elements like the above ones
I have access to xalan:evaluate to evaluate the dynamic xpath. I'm trying different solutions, I will write them here when they will start to make sense.
Any ideas of approaches are well received. Thanks
Oki, I found out how, and I will write the answer here maybe someone sometime will need this:
<xsl:template match="/">
<!-- static parents -->
<a:Root>
<xsl:apply-templates select="/a:Root/a:Parent" />
</a:Root>
</xsl:template>
<xsl:template match="#*|*|text()">
<xsl:variable name="x" select="generate-id(../.)" />
<xsl:variable name="y" select="//field[generate-id(xalan:evaluate(xpath)) = $x]" />
<xsl:choose>
<xsl:when test="$y">
<xsl:value-of select="$y/newValue" />
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="#*|*|text()" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
And to explain the transformation:
I'm writing down part that is static and then call apply-templates on the fragment I'm interested in, that has a liquid structure.
Then I'm using a slightly modified identity transformation that copies everything from source to target (starting from the /a:Root/a:Parent fragment), except when we position ourselves on the text I'm interested in changing.
The text() I'm interested in will have as parent (../.) the element referred by an xpath string found in the second fragment. Variable x means, in the context of the when, this element.
Variable y finds a field element that has as child an xpath element that if evaluated using xalan will refer to the same element that the x variable relates to.
Now I used generate-id() in order to compare the physical elements, otherwise it would have compared by the toString of the element (which is wrong). If variable y doesn't exist, it means that I have no xpath element for this element that could have changed, and I'm leaving it alone. If the y variable exists, I can get from it the newValue and I'm currently positioned on the element which text I want to update.

Filter and sort table based XML

I am new to xslt and have done some research and read a brief book and looked at many examples but I'm afraid I just don't get it. I've only done simple procedural coding before and I guess I'm missing something. I understand a very basic example but when I try to transform my own data I am completely lost. Boo hoo hoo. It is soooo frustrating knowing that I don't it! I really feel like a hack :(
Anyway, I generated the following XML from a table in MS Word. The table ID and Row and Column IDs of each cell are given so it is possible to know how things relate to each other.
Now I want to present the data in a pick list and basically want to:
1. Filter the data on, say [Name='p_fld_parent_ref' and Value='RM12']. The data with the matching "rows" (i.e. all the nodes with the matching RowID) is what I want.
2. I also want to sort that filtered data on the column (cell) with name [p_fld_date_received]. I included a #dateSerial attribute specifically to make the sorting easier.
In the example data I should get any "rows" of data with a parent reference of 'RM12' sorted by date received. I want to use the data in the [p_fld_quantity_available] cell.
I've wasted about 3 days on this and gotten absolutely nowhere. Normally you start to get somewhere but with xslt I've gotten nowhere. Strange.
Here is one "row" of my data (sorry, I pasted the XML but don't know how to format it for reading - can someone let me know how to display it in the right format? Thanks.):
<Root><Data><Element><Name>p_fld_ref</Name><Key>SKU1</Key><KeyType>P</KeyType><ID>1</ID><Value>SKU1</Value><Description>Ref</Description><Required>True</Required><dataType>SKUn</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>1</ColumnID></Element><Element><Name>p_fld_parent_ref</Name><Key>SKU1</Key><KeyType>F</KeyType><ID>2</ID><Value>RM12</Value><Description>Parent Ref</Description><Required>True</Required><dataType>CPLn,RMn</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>2</ColumnID></Element><Element><Name>p_fld_item_no</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>3</ID><Value>ZRMH06</Value><Description>Item Code</Description><Required>True</Required><dataType>Text</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>3</ColumnID></Element><Element><Name>p_fld_type</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>4</ID><Value>RM</Value><Description>Type</Description><Required>True</Required><dataType>CA or RM</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>4</ColumnID></Element><Element><Name>p_fld_serial_no</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>5</ID><Value>120201</Value><Description>Serial No.</Description><Required>True</Required><dataType>Number</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>5</ColumnID></Element><Element><Name>p_fld_name_1</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>6</ID><Value>Product Name 1</Value><Description>Name 1</Description><Required>True</Required><dataType>Text</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>6</ColumnID></Element><Element><Name>p_fld_name_2</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>7</ID><Value>Product Name 1 Lang 2</Value><Description>Name 2</Description><Required>True</Required><dataType>Text</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>7</ColumnID></Element><Element><Name>p_fld_location</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>8</ID><Value/><Description>Location</Description><Required>True</Required><dataType>Text</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>8</ColumnID></Element><Element><Name>p_fld_receipt_no</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>9</ID><Value/><Description>Receipt No.</Description><Required>True</Required><dataType>Text</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>9</ColumnID></Element><Element><Name>p_fld_supplier</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>10</ID><Value/><Description>Supplier</Description><Required>True</Required><dataType>Text</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>10</ColumnID></Element><Element><Name>p_fld_supplier_ref</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>11</ID><Value/><Description>Supplier Ref</Description><Required>True</Required><dataType>Sn</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>11</ColumnID></Element><Element><Name>p_fld_supplier_batchno</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>12</ID><Value/><Description>Supplier Batch No.</Description><Required>True</Required><dataType>Text</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>12</ColumnID></Element><Element><Name>p_fld_supplier_coa</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>13</ID><Value/><Description>CoA Number</Description><Required>True</Required><dataType>Text</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>13</ColumnID></Element><Element><Name>p_fld_box_sample</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>14</ID><Value/><Description>Box to sample</Description><Required>True</Required><dataType>Text</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>14</ColumnID></Element><Element><Name>p_fld_number_boxes</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>15</ID><Value/><Description>Number of Containers</Description><Required>True</Required><dataType>Number</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>15</ColumnID></Element><Element><Name>p_fld_quantity_total</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>16</ID><Value/><Description>Total Quantity</Description><Required>True</Required><dataType>Number</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>16</ColumnID></Element><Element><Name>p_fld_date_received</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>17</ID><Value dateSerial="20130217000000">17-Feb-2013</Value><Description>Date Received</Description><Required>True</Required><dataType>Date</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>17</ColumnID></Element><Element><Name>p_fld_date_retest</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>18</ID><Value dateSerial="20140217000000">17-Feb-2014</Value><Description>Re-Test Date</Description><Required>True</Required><dataType>Date</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>18</ColumnID></Element><Element><Name>p_fld_date_expire</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>19</ID><Value dateSerial="20160217000000">17-Feb-2016</Value><Description>Expiry Date</Description><Required>True</Required><dataType>Date</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>19</ColumnID></Element><Element><Name>p_fld_date_released</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>20</ID><Value dateSerial="20130217000000">17-Feb-2013</Value><Description>Release Date</Description><Required>True</Required><dataType>Date</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>20</ColumnID></Element><Element><Name>p_fld_quantity_reserved</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>21</ID><Value>20000</Value><Description>Reserved Quantity</Description><Required>True</Required><dataType>Number</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>21</ColumnID></Element><Element><Name>p_fld_quantity_used</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>22</ID><Value>0</Value><Description>Used Quantity</Description><Required>True</Required><dataType>Number</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>22</ColumnID></Element><Element><Name>p_fld_quantity_available</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>23</ID><Value>20000</Value><Description>Available Quantity</Description><Required>True</Required><dataType>Number</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>23</ColumnID></Element><Element><Name>p_fld_status</Name><Key>SKU1</Key><KeyType>D</KeyType><ID>24</ID><Value/><Description>Status</Description><Required>True</Required><dataType>Q, R or X</dataType><parmType>1</parmType><TableID>6</TableID><RowID>3</RowID><ColumnID>24</ColumnID></Element></Root>
Assuming that each row is represented by a <Data> element (incidentally, you're missing the end tag of the <Data> element in this sample), this should do the filter/sorting part:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Root">
<Root>
<xsl:apply-templates
select="Data[Element[Name='p_fld_parent_ref']/Value='RM12']">
<xsl:sort select="Element[Name='p_fld_date_received]/Value/#dateSerial" />
</xsl:apply-templates>
</Root>
</xsl:template>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
You'll need to define a template for how you want your <Data> elements presented, and probably something other than simply re-creating the Root element as I've done above. Something as simple as this might do the trick, if you just want to output text:
<xsl:template match="Data">
<xsl:text>Item no.:</xsl:text>
<xsl:value-of select="Element[Name='p_fld_item_no']/Value" />
<xsl:text> Quantity available:</xsl:text>
<xsl:value-of select="Element[Name='p_fld_quantity_available']/Value" />
<xsl:text>
</xsl:text><!-- New line -->
</xsl:template>
This isn't a complete solution, but hopefully it'll give you enough pointers to figure out the rest.