I have a following XML file:
<titles>
<book title="XML Today" author="David Perry"/>
<book title="XML and Microsoft" author="David Perry"/>
<book title="XML Productivity" author="Jim Kim"/>
<book title="XSLT 1.0" author="Albert Jones"/>
<book title="XSLT 2.0" author="Albert Jones"/>
<book title="XSLT Manual" author="Jane Doe"/>
</titles>
I want to eliminate some elements and apply the following XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="author1-search" match="book[starts-with(@author, 'David')]" use="@title"/>
<xsl:template match="book [key('author1-search', @title)]" />
<xsl:key name="author2-search" match="book[starts-with(@author, 'Jim')]" use="@title"/>
<xsl:template match="book [key('author2-search', @title)]" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
Is it possible to use an inline xsl variable
<xsl:variable name="Author">
<name>David</name>
<name>Jim</name>
</xsl:variable>
instead of "author1-search", "author2-search", and so on to loop through names?
I can use only XSLT 1.0 (2.0 is currently not supported).
Thanks in advance,
Leo
No, patterns (in XSLT 1.0) cannot contain variable/parameter references.
One way to perform such a task would be like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="pAuthor" select="'David Perry'"/>
<xsl:key name="kBookaByAuthor" match="book"
use="@author"/>
<xsl:template match="/">
Books written by <xsl:value-of select="$pAuthor"/> :<xsl:text/>
<xsl:apply-templates
select="key('kBookaByAuthor', $pAuthor)"/>
</xsl:template>
<xsl:template match="book">
<!-- One can do more formatting here -->
<xsl:text>
 </xsl:text>
<xsl:value-of select="@title"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<titles>
<book title="XML Today" author="David Perry"/>
<book title="XML and Microsoft" author="David Perry"/>
<book title="XML Productivity" author="Jim Kim"/>
<book title="XSLT 1.0" author="Albert Jones"/>
<book title="XSLT 2.0" author="Albert Jones"/>
<book title="XSLT Manual" author="Jane Doe"/>
</titles>
the wanted, correct result is produced:
Books written by David Perry :
XML Today
XML and Microsoft
Update: In a comment the OP has clarified that:
"I thought I fully specified my requirements in the initial question.
As I mentioned in my question and in my first comment, it would be
helpful to me to see the approach for dealing with more than one
author"
Here is a solution that truly uses keys (note that the "key" in the answer by @Flynn1179 doesn't build any index and is just a constant sequence of strings-- so the function key()
using that xsl:key
is actually finding a string in a list of strings -- which is O(N) as contrasted to, typically, O(1) for searching in a true index):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common">
<xsl:output method="text"/>
<xsl:param name="pAuthors">
<x>David Perry</x>
<x>Jane Doe</x>
</xsl:param>
<xsl:variable name="vParams" select=
"ext:node-set($pAuthors)/*"/>
<xsl:key name="kBookByAuthor" match="book"
use="@author"/>
<xsl:template match="/">
Books written by : <xsl:text/>
<xsl:apply-templates select="$vParams"/>
<xsl:apply-templates select=
"key('kBookByAuthor', $vParams)"/>
</xsl:template>
<xsl:template match="book">
<!-- One can do more formatting here -->
<xsl:text>
 </xsl:text>
<xsl:value-of select="concat('"', @title, '"')"/>
</xsl:template>
<xsl:template match="x">
<xsl:if test="not(position() = 1)">, </xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied to the provided XML document (above), the wanted, correct result is produced:
Books written by : David Perry, Jane Doe
"XML Today"
"XML and Microsoft"
"XSLT Manual"
Do note: In this solution the Exslt function node-set()
is used. This is done only for convenience here. In a real usage, the value of the parameter will be specified externally and then the ext:node-set()
function isn't necessary.
Efficiency: This solution uses the true power of keys in XSLT. An experiment made using MSXML (3, 4 and 6) XSLT processors shows that if we search for 10000 authors the transformation time with different XSLT processors ranges from: 32ms to 45ms.
Interestingly, the solution presented by @Flynn1179 doesn't indeed make key index and with many XSLT processors it takes (for the same number (10000) of authors) from 1044ms to 5564ms:
MSXML3: 5564 ms.,
MSXML4: 2526ms,
MSXML6: 4867 ms,
AltovaXML: 1044ms.
This is quite inferior to the performance one gets with true key indexing (32ms to 45ms).
Patterns in XSLT 1.0 are not allowed to contain variable or parameter references so you couldn't use variable or parameter references in those key definitions or in the template match attributes you have.
Rather than using variables, you could just include an element in your XSLT sheet in it's own namespace, and refer to that, like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my">
<xsl:key name="authors" use="document('')/*/my:authors/my:name" match="/" />
<my:authors>
<my:name>David Perry</my:name>
<my:name>Jim Kim</my:name>
</my:authors>
<xsl:template match="book[not(key('authors',@author))]" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The book
template matches those that do NOT have a corresponding my:name
element for their author, and outputs nothing. The identity template outputs everything else, including the book elements you DO care about. The key's a bit of a hack, it essentially matches the whole document where the name exists, rather than matching the my:name
element that matches. Since you only care about it's existence, this shouldn't be a problem.
Alternatively, if you'd rather be able to pass in a list of authors, you can use this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="authors" select="'David Perry,Jim Kim'" />
<xsl:template match="book">
<xsl:if test="contains(concat(',',$authors,','),concat(',',@author,','))">
<xsl:call-template name="identity" />
</xsl:if>
</xsl:template>
<xsl:template match="@* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The variable's used in an <xsl:if>
rather than in the template match, but it does the same job. This particular code needs the list of authors specified as a comma separated list, but it should be easy enough to adapt it if you'd rather use a different format.