xslt - Select from within a fileliste of xml -
i use xslt 2.0 transform xml data xml files. created xsl find filenames in folder, worked fine:
filenames.xml
<?xml version="1.0" encoding="utf-8"?> <filelist> <file>birds2014.xml</file> <file>birds2013.xml</file> </filelist>
then created xsl search within xml-files select names of birds located in tag <spbird>
in documents.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" exclude-result-prefixes="xs" version="2.0"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="*"> <xsl:apply-templates/> <xsl:variable name="files" select="document('filelist.xml')/filelist/file"/> <xsl:element name="birdname"> <xsl:for-each select="document($files)"> <xsl:value-of select="//spbird"> </xsl:value-of> </xsl:for-each> </xsl:element> </xsl:template>
result:
<birdname>papagei wellensittich pfau</birdname> <birdname>papagei wellensittich pfau</birdname> <birdname>papagei wellensittich pfau</birdname> <birdname>papagei wellensittich pfau</birdname> <birdname>papagei wellensittich pfau</birdname> <birdname>papagei wellensittich pfau</birdname> <birdname>papagei wellensittich pfau</birdname> <birdname>papagei wellensittich pfau</birdname> <birdname>papagei wellensittich pfau</birdname>
expected:
<birdname>papagei</birdname> <birdname>wellensittich</birdname> <birdname>pfau</birdname>
can help?
edit changed
<xsl:template match="*">
to
<xsl:template match="/">
which resolves issue of same result showing 9 times (where did number came from).
now result is:
<birdname>papagei wellensittich</birdname> <birdname>pfau</birdname>
pfau
bird in second xml file. problem is, document() seems for-each on it's own. not birds listed seperately in birdname tag..
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" exclude-result-prefixes="xs" version="2.0"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="*"> <xsl:apply-templates/> <xsl:variable name="files" select="document('filelist.xml')/filelist/file"/> <xsl:for-each select="document($files)"> <birdname> <xsl:value-of select="//spbird"> </birdname> </xsl:value-of> </xsl:for-each> </xsl:element> </xsl:template>
try putting element inside of xsl:for-each
...
<xsl:for-each select="document($files)"> <birdname> <xsl:value-of select="//spbird"/> </birdname> </xsl:for-each>
note: unless you're trying dynamically build element name, there's no reason use xsl:element
.
edit
in xslt 2.0, xsl:value-of
give every value of spbird
. instead of xsl:for-each
on document, should for-each on spbird
. try changing xsl:for-each
example below. if doesn't work, please add example of bird files question.
<xsl:for-each select="document($files)//spbird"> <birdname> <xsl:value-of select="."/> </birdname> </xsl:for-each>
Comments
Post a Comment