xslt-2.0 - SaxonApiException : The context item for axis step ./客户端不存在

标签 xslt-2.0 saxon

我正在尝试使用 saxon/java 中的 XSLT 2.0 将 XML 转换为 XML。我正在使用在堆栈溢出线程“Applying Muenchian grouping for a simple XML with XSLT”中找到的示例 XML

但是我收到错误:XPDY0002:轴步骤 ./CLIENT 的上下文项不存在。

我的测试 XSL:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="CLIENTS" name="main">
<CLIENTS>
  <xsl:for-each-group select="CLIENT" group-by="NAME">
  <xsl:comment><xsl:value-of select="current-grouping-key()"/>         </xsl:comment>
    <CLIENT>
      <xsl:sequence select="NAME" />
      <xsl:for-each select="current-group()">
        <ACCOUNT>
          <xsl:sequence select="*[not(self::NAME)]" />
        </ACCOUNT>
      </xsl:for-each>
    </CLIENT>
  </xsl:for-each-group>
 </CLIENTS>
</xsl:template>

</xsl:stylesheet>

我的测试 XML:

<CLIENTS>
 <CLIENT>
<NAME>John</NAME>
<ACCOUNT_NUMBER>1424763562761</ACCOUNT_NUMBER>
<LAST_USED>2012-10-03</LAST_USED>
<AMOUNT>5000</AMOUNT>
</CLIENT>
<CLIENT>
<NAME>John</NAME>
<ACCOUNT_NUMBER>543667543732</ACCOUNT_NUMBER>
<LAST_USED>2012-10-02</LAST_USED>
<AMOUNT>10000</AMOUNT>
</CLIENT>
</CLIENTS>

我的 Java(可与其他转换一起使用):

void xmlXSLTParser(){

String xslFile = commonPath + "/xslt/inputPointCSVTOXML_style2.xsl";
String inputFile = "file:///" + commonPath + pointWorkFile;
String outputFile = commonPath + pointWorkFile + ".final";

try {
    Processor proc = new Processor(false);
    XsltCompiler comp = proc.newXsltCompiler();
    XsltExecutable exp = comp.compile(new StreamSource(new   File(xslFile)));
    Serializer out = new Serializer();
    out.setOutputProperty(Serializer.Property.METHOD, "xml");
    out.setOutputProperty(Serializer.Property.INDENT, "yes");
    out.setOutputFile(new File(outputFile));

    XsltTransformer trans = exp.load();
    trans.setInitialTemplate(new QName("main"));
    //trans.setParameter(new QName("url-of-csv"),new  XdmAtomicValue(inputFile));
    trans.setDestination(out);
    trans.transform();

    System.out.println("Output written to text file");
} catch (SaxonApiException e) {
    println("XSLT Error :" + e );
}
}

}

我的错误详细信息:

Error at char 6 in xsl:for-each-group/@select on line 10 column 59 of    inputPointCSVTOXML_style2.xsl:

XPDY0002: The context item for axis step ./CLIENT is absent
XSLT Error :net.sf.saxon.s9api.SaxonApiException: The context item for      axis step ./CLIENT is absent

最佳答案

您的 Java 代码不会设置任何上下文项,而是设置一个初始模板。因此,您需要确保将输入 XML 作为上下文项提供给 XsltTransformer,使用 http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltTransformer.html#setInitialContextNode(net.sf.saxon.s9api.XdmNode)或作为,使用 http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltTransformer.html#setSource(javax.xml.transform.Source) .

因此,不要使用 trans.setInitialTemplate(new QName("main"));,而是使用 trans.setSource(new StreamSource(inputFile));

关于xslt-2.0 - SaxonApiException : The context item for axis step ./客户端不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39063455/

相关文章:

xml - 是否可以用另一个 XSLT 代码(元 XSLT)转换 XSLT 代码?

xslt - XSL 查找节点之间的所有节点

xml - xslt : How can I apply two templates to the same node during processing?

xml - 如何在 Saxon-HE 中获得 EXSLT 支持?

xml - 我如何使用 Saxon 以高效的方式对值进行多次搜索/替换

java - 如何启用 Saxon xpath 表达式缓存?

validation - Grails 根据 xsd 1.1 验证 xml 文档

xml - 默认命名空间未正确转换

java - 解析 XSL 时 Saxon 中的 NullPointerException

xslt - 使用 Saxon 读取 XSLT 样式表中的环境变量