[xslt] key() doesn't work in conjunction with copied documents


I've encountered a strange problem when applying a stylesheet on a copied document. Here is an example consisting of two input files, the stylesheet, and the application.

application "test.cc":
#include <libxslt/xsltutils.h>
#include <libxslt/transform.h>

int main () {
  xsltStylesheetPtr cur;
  xmlDocPtr doc;

  cur = xsltParseStylesheetFile((xmlChar*)"script.xsl");
  doc = xmlReadFile("doc1.xml",0,XSLT_PARSE_OPTIONS);
  doc = xmlCopyDoc(doc,1);

  xsltTransformContextPtr ctxt = xsltNewTransformContext(cur,doc);
  xmlDocPtr res = xsltApplyStylesheetUser(cur,doc,NULL,NULL,NULL,ctxt);
  return 0;

The application is quite simple: After loading the stylesheet "script.xsl" and the input document "doc1.xml" the input document is copied using xmlCopyDoc() and then the stylesheet is applied on that copy. The result is saved to "out.txt".

input file "doc1.xml":
<?xml version="1.0"?>

input file "doc2.xml":
<?xml version="1.0"?>

stylesheet "script.xsl":
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"

  <xsl:output method="text"/>
  <xsl:variable name="root" select="/"/>
  <xsl:key name="node-key" match="node" use="name()"/>

  <xsl:template match="/">
    <xsl:for-each select="document('doc2.xml')">
      <xsl:for-each select="$root">
        <xsl:value-of select="count(key('node-key','node'))"/>

The script is equally simple: After loading a second input file and instantly resetting the current document to the old input file, key() is called and the number of matches is printed.

The problem here is that key() doesn't match anything. But if I remove "doc = xmlCopyDoc(doc,1);" from the application, i.e. the stylesheet is applied to the original input document, key() works as expected.

Is this a bug or how do I copy a document without parsing the input file again and without loosing the functionality of key()?


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]