SaxonApiException:轴步骤./CLIENT的上下文项不存在

时间:2016-08-21 11:00:47

标签: xslt-2.0 saxon

我正在尝试使用saxon / java中的XSLT 2.0将XML转换为XML。我正在使用我在堆栈溢出线程上找到的示例XML“将Muenchian分组应用于使用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

1 个答案:

答案 0 :(得分:2)

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

而不是trans.setSource(new StreamSource(inputFile));使用gvTest