SaxonApiException: The context item for axis step ./CLIENT is absent

那年仲夏 提交于 2021-01-27 19:46:55

问题


I am trying to convert and XML to an XML using XSLT 2.0 in saxon/java. I am using a sample XML I found on stack overflow thread "Applying Muenchian grouping for a simple XML with XSLT"

However I am getting an error : XPDY0002: The context item for axis step ./CLIENT is absent.

My test 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>

My Test 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>

My Java ( which works with other transforms) :

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

}

My Error in detail:

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:


Your Java code does not set any context item, instead it sets an initial template. So you will need to make sure you provide the input XML as the context item to the XsltTransformer, using http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltTransformer.html#setInitialContextNode(net.sf.saxon.s9api.XdmNode) or as a Source, using http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltTransformer.html#setSource(javax.xml.transform.Source).

So instead of trans.setInitialTemplate(new QName("main")); use trans.setSource(new StreamSource(inputFile));.



来源:https://stackoverflow.com/questions/39063455/saxonapiexception-the-context-item-for-axis-step-client-is-absent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!