Different XSLT processing result compared with xsltproc

423 views Asked by At

I'm doing XSLT processing with libxslt(plus libxml2, libexslt).
First, I did my XSLT processing with xsltproc using an XML input file like below and using MS Office's XSL file(APASixthEditionOfficeOnline.xsl). You can see the XML output like below.

XML input (input.xml)

<?xml version="1.0"?>
<b:StyleNameLocalized xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography">
    <b:Lcid>1042</b:Lcid>
</b:StyleNameLocalized>`

XSL stylesheet (APASixthEditionOfficeOnline.xsl)

<?xml version="1.0" encoding="utf-8"?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography">
    <xsl:output method="html" encoding="us-ascii"/>
    <xsl:template match="/">
      <xsl:choose>
        <xsl:when test="b:StyleNameLocalized">
          <xsl:choose>
            <xsl:when test="b:StyleNameLocalized/b:Lcid='1042'">
              <xsl:text>APA</xsl:text>
            </xsl:when>
          </xsl:choose>
        </xsl:when>
      </xsl:choose>             
    </xsl:template>
  </xsl:stylesheet>

XML output by xsltproc

  • I wrote the below code in the command line.

    xsltproc APASixthEditionOfficeOnline.xsl input.xml > output.xml

  • And I got a text in output.xml
    APA


Meanwhile, I tried to make my own xsltproc by implementing the functions of libxml2 and libxslt.

I used the same APASixthEditionOfficeOnline.xsl file, however didn't parsed the input.xml but generated XmlDocPtr in the code. Below is my code.

My Code

const xmlChar* stylesheetfile = (const xmlChar*)"APASixthEditionOfficeOnline.xsl";
xsltStylesheetPtr style = xsltParseStylesheetFile(xslfile);
xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
xmlNodePtr root_node = xmlNewNode(NULL, BAD_CAST "b:StyleNameLocalized");
xmlNsPtr ns =
        xmlNewNs(root_node,
                BAD_CAST "http://schemas.openxmlformats.org/officeDocument/2006/bibliography",
                BAD_CAST "b");
xmlDocSetRootElement(doc, root_node);
xmlNewChild(root_node, ns, BAD_CAST "Lcid", BAD_CAST "1042");
xmlDocPtr output = xsltApplyStylesheet(style, doc, 0);
mlChar* xmlData;
int size;
xmlDocDumpMemory(output, &xmlData, &size);

The expected result of the variable xmlData is "APA", but I got this result.

My Result
<?xml version="1.0" encoding="us-ascii" standalone="yes"?>

I want the same result of xsltproc.
Can you figure out the problem of my code?
It will be really helpful for your comment.
Thank you.

1

There are 1 answers

2
nwellnhof On BEST ANSWER

Since the result of an XSLT transformation isn't always a well-formed document and depends on xsl:output, you have to use one of the xsltSaveResultTo functions to output the result.