xsl-list
[Top] [All Lists]

Re: [xsl] filtering problem

2010-06-17 04:15:38

Perhaps something like this:

<xsl:variable name="firstFailure" 
select="(//command[(_at_)failed='true()])[1]"/>

<xsl:variable name="pathToFirstFailure" select="$firstFailure/ancestor-or-self::*"/>

<xsl:template match="*[. intersect $pathToFirstFailure]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="*"/>

You don't see this coding pattern very often, but I sometimes find it quite useful: select the "interesting" nodes in a global variable, and then use this global variable in a match pattern to steer which template rule is used to process which nodes. In XSLT 2.1 you will be able to replace the match pattern above with

match="$pathToFirstFailure"

Michael Kay
Saxonica

On 17/06/2010 09:54, Adrian Herscu wrote:
Hi all,

Consider the following input XML file:

<?xml version="1.0" encoding="UTF-8"?>
<batch name="level1">
<command src="1" />
<command src="2" failed="true()" />
<command src="3" />
<command src="4" failed="true()" />
<batch name="level2">
<command src="10" />
<command src="11" failed="true()" />
<command src="12" />
<batch name="level3">
<command src="20" />
<command src="21" failed="true()" />
<command src="22" />
</batch>
<command src="30" />
<command src="31" failed="true()" />
<command src="32" />
</batch>
</batch>

I need an XSL transformation that scans the top-level batch until it finds a command marked as failed, no matter at which internal batch-level it is, preserving the batch level on the report.

Currently, I have this XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xalan="http://xml.apache.org/xalan";>

<xsl:output method="xml" indent="yes" encoding="UTF-8"
    media-type="text/xml" xalan:indent-amount="2" />

<xsl:strip-space elements="*" />

<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<!--
    FIXME: batches are matched even though there is a prior failed
    command or batch containing a failed command
  -->
<xsl:template match="batch">
<batch name="{(_at_)name}">
<xsl:apply-templates />
</batch>
</xsl:template>

<!-- match only the first failing command -->
<xsl:template match="command[(_at_)failed=true()][1]">
<failed-command src="{(_at_)src}" />
</xsl:template>
</xsl:stylesheet>

which generates:

<?xml version="1.0" encoding="UTF-8"?>
<batch xmlns:xalan="http://xml.apache.org/xalan"; name="level1">
<failed-command src="2" />
<batch name="level2">
<failed-command src="11" />
<batch name="level3">
<failed-command src="21" />
</batch>
</batch>
</batch>

instead of:

<?xml version="1.0" encoding="UTF-8"?>
<batch xmlns:xalan="http://xml.apache.org/xalan"; name="level1">
<failed-command src="2" />
</batch>

Another example -- if only command[(_at_)src='21'] is marked failed, then the generated output should be:

<?xml version="1.0" encoding="UTF-8"?>
<batch name="level1">
<batch name="level2">
<batch name="level3">
<failed-command src="21" />
</batch>
</batch>
</batch>

Thanks for your help,
Adrian.


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>