2

我正在使用 libxslt(加上 libxml2、libexslt)进行 XSLT 处理。
首先,我xsltproc使用如下所示的 XML 输入文件和 MS Office 的 XSL 文件 (APASixthEditionOfficeOnline.xsl) 进行了 XSLT 处理。您可以看到如下所示的 XML 输出。

XML 输入 (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 样式表 (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 输出xsltproc

  • 我在命令行中编写了以下代码。

    xsltproc APASixthEditionOfficeOnline.xsl input.xml > output.xml

  • 我在 output.xml
    APA中得到了一个文本


同时,我尝试通过实现libxml2和libxslt的功能来制作自己的xsltproc。

我使用了相同的 APASixthEditionOfficeOnline.xsl 文件,但是没有解析 input.xml 而是在代码中生成了 XmlDocPtr。下面是我的代码。

我的代码

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);

变量的预期结果xmlData是“APA”,但我得到了这个结果。

我的结果
<?xml version="1.0" encoding="us-ascii" standalone="yes"?>

我想要 xsltproc 的相同结果。
你能找出我的代码的问题吗?
这对您的评论非常有帮助。
谢谢你。

4

1 回答 1

1

由于 XSLT 转换的结果并不总是格式良好的文档并且依赖于xsl:output,因此您必须使用其中一个xsltSaveResultTo函数来输出结果。

于 2018-11-29T11:20:07.923 回答