带有 XSL:排序而不是排序的 XML



我有一个xml文档,我需要按日期排序。我有一个调用 xsl 样式表模板的 java 程序。输出只是相同的 xml,但应按日期降序排序。排序不起作用。我得到的结果是输出看起来与输入相同。

下面是源 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>

爪哇岛:

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>

如果要对ABCDocuments进行排序,则需要再高一级停止。您还需要正确使用命名空间,因为源 XML 使用命名空间:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ex="http://www.example.com/Schema">
  <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="ex:ABCDocumentList">
    <xsl:copy>
      <xsl:apply-templates select="ex:ABCDocument">
        <xsl:sort select="ex:EffectiveDate" data-type="number" order="descending"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

最新更新