Saxon HE 9.2を使用して XSLT 変換を実行し、出力は後でCastor 1.3.1によって非整列化されます。すべてがJDK 6の Java で実行されます。
私の XSLT 変換は次のようになります。
<xsl:transform
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://my/own/custom/namespace/for/the/target/document">
<xsl:output method="xml" encoding="UTF-8" indent="no" />
<xsl:template match="/">
<ns:item>
<ns:property name="id">
<xsl:value-of select="/some/complicated/xpath" />
</ns:property>
<!-- ... more ... -->
</ns:item>
</xsl:template>
つまり、XPath 式/some/complicated/xpath
が空のシーケンスに評価される場合、Saxon シリアライザーは<ns:property/>
代わりに を書き込みます<ns:property></ns:property>
。ただし、これはパイプラインの次のキャスター アンマーシャラーを混乱させ、変換の出力を XSD で生成された Java クラスのインスタンスにアンマーシャリングします。
私の質問は次のとおりです。Saxon-serializer に、スタンドアロンのタグとしてではなく空のタグを出力するように指示するにはどうすればよいですか?
変換を実行するために私が現在行っていることは次のとおりです。
import net.sf.saxon.s9api.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXSource;
// ...
// read data
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
// ... there is some more setting up the xmlReader here ...
InputStream xsltStream = new FileInputStream(xsltFile);
InputStream inputStream = new FileInputStream(inputFile);
Source xsltSource = new SAXSource(xmlReader, new InputSource(xsltStream));
Source inputSource = new SAXSource(xmlReader, new InputSource(inputStream));
XdmNode input = processor.newDocumentBuilder().build(inputSource);
// initialize transformation configuration
Processor processor = new Processor(false);
XsltCompiler compiler = processor.newXsltCompiler();
compiler.setErrorListener(this);
XsltExecutable executable = compiler.compile(xsltSource);
Serializer serializer = new Serializer();
serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
serializer.setOutputProperty(Serializer.Property.INDENT, "no");
serializer.setOutputStream(output);
// execute transformation
XsltTransformer transformer = executable.load();
transformer.setInitialContextNode(input);
transformer.setErrorListener(this);
transformer.setDestination(serializer);
transformer.setSchemaValidationMode(ValidationMode.STRIP);
transformer.transform();
解決策の方向性を示すヒントをいただければ幸いです。:-) 不明な点がある場合は、喜んで詳細をお知らせします。