0
我有一個XML文件,我需要按日期排序。我有一個調用xsl樣式表模板的java程序。輸出只是相同的XML,但應按日期降序排序。排序不起作用。我得到的結果是輸出看起來與輸入相同。xml與xsl:排序不排序
這裏是源XML的樣子:
<?xml version="1.0"?>
<ABCResponse xmlns="http://www.example.com/Schema">
<ABCDocumentList>
<ABCDocument>
<DocumentType>APPLICATION</DocumentType>
<EffectiveDate>20140110010000</EffectiveDate>
<Name>JOE DOCS</Name>
</ABCDocument>
<ABCDocument>
<DocumentType>FORM</DocumentType>
<EffectiveDate>20140206010000</EffectiveDate>
<Name>JOE DOCS</Name>
</ABCDocument>
<ABCDocument>
<DocumentType>PDF</DocumentType>
<EffectiveDate>20140120010000</EffectiveDate>
<Name>JOE DOCS</Name>
</ABCDocument>
</ABCDocumentList>
</ABCResponse>
的Java:
import java.io.File;
import java.io.StringReader;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class TestMisc {
/**
* @param args
*/
public static void main(String[] args) {
Source xmlInput = new StreamSource(new File("c:/temp/ABCListDocsResponse.xml"));
Source xsl = new StreamSource(new File("c:/temp/DocResponseDateSort.xsl"));
Result xmlOutput = new StreamResult(new File("c:/temp/ABC_output1.xml"));
try {
javax.xml.transform.TransformerFactory transFact =
javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer trans = transFact.newTransformer(xsl);
trans.transform(xmlInput, xmlOutput);
} catch (TransformerException e) {
}
}
}
這裏是
XSL的XSL:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ABCResponse/ABCDocumentList/ABCDocument">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:sort select="EffectiveDate" data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我改變了我的樣式表來停止一個水平像建議,但輸出XML仍然是沒有排序。 – user3187765
XSLT也需要正確使用名稱空間。答案已更新。 – JLRishe