转换XML时编译样式表错误

时间:2019-12-19 18:14:05

标签: java xml xslt

我正在尝试使用XSLT转换XML。

下面是我的代码,XML和XSLT

参数:

stylesheetPathname: /home/Jon/XSLT.xsl
inputPathname:/home/Jon/countries.xml
outputPathname:/home/Jon/cTransformedXML.xml

代码:

public static void xmlProcessor(String stylesheetPathname, String inputPathname, String outputPathname ) throws TransformerException {

    TransformerFactory factory = TransformerFactory.newInstance();
    Source stylesheetSource = new StreamSource(new File(stylesheetPathname));
    Transformer transformer = factory.newTransformer(stylesheetSource);
    Source inputSource = new StreamSource(new File(inputPathname));
    Result outputResult = new StreamResult(new File(outputPathname));
    transformer.transform(inputSource, outputResult);

}

XML

<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE countries SYSTEM "countries.dtd"> -->
<!-- xmlns="http://www.example.org/countries" -->
<countries>
  <!-- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:noNamespaceSchemaLocation="countries.xsd" -->
  <country name="Afghanistan" population="22664136" area="647500">
    <language percentage="11">Turkic</language>
    <language percentage="35">Pashtu</language>
    <language percentage="50">Afghan Persian</language>
  </country>
  <country name="Albania" population="3249136" area="28750" />
  <country name="Algeria" population="29183032" area="2381740">
    <city>
      <name>Algiers</name>
      <population>1507241</population>
    </city>
  </country>
  <country name="American Samoa" population="59566" area="199" />
  <country name="Andorra" population="72766" area="450" />
  <country name="Angola" population="10342899" area="1246700" />
  <country name="Anguilla" population="10424" area="91">
    <language percentage="100">English</language>
  </country>
  <country name="Antigua and Barbuda" population="65647" area="440">
    <language percentage="100">English</language>
  </country>
  <country name="Argentina" population="34672996" area="2766890">
    <city>
      <name>La Matanza</name>
      <population>1111811</population>
    </city>
    <city>
      <name>Cordoba</name>
      <population>1208713</population>
    </city>
    <city>
      <name>Rosario</name>
      <population>1118984</population>
    </city>
    <city>
      <name>Buenos Aires</name>
      <population>2988006</population>
    </city>
  </country>
  <country name="Armenia" population="3463574" area="29800">
    <city>
      <name>Yerevan</name>
      <population>1200000</population>
    </city>
    <language percentage="2">Russian</language>
    <language percentage="96">Armenian</language>
  </country>
  <country name="Aruba" population="67794" area="193" />
  <country name="Australia" population="18260864" area="7686850">
    <city>
      <name>Sydney</name>
      <population>3657000</population>
    </city>
    <city>
      <name>Brisbane</name>
      <population>1302000</population>
    </city>
    <city>
      <name>Adelaide</name>
      <population>1050000</population>
    </city>
    <city>
      <name>Melbourne</name>
      <population>3081000</population>
    </city>
    <city>
      <name>Perth</name>
      <population>1193000</population>
    </city>
    <language percentage="100">English</language>
  </country>
  <country name="Austria" population="8023244" area="83850">
    <city>
      <name>Vienna</name>
      <population>1583000</population>
    </city>
    <language percentage="100">German</language>
  </country>
  <country name="Azerbaijan" population="7676953" area="86600">
    <city>
      <name>Baku</name>
      <population>1740000</population>
    </city>
    <language percentage="3">Russian</language>
    <language percentage="2">Armenian</language>
    <language percentage="89">Azeri</language>
  </country>
  <country name="Bahamas" population="259367" area="13940" />
  <country name="Bahrain" population="590042" area="620" />
  <country name="Bangladesh" population="123062800" area="144000">
    <city>
      <name>Dhaka</name>
      <population>3839000</population>
    </city>
    <city>
      <name>Chittagong</name>
      <population>1599000</population>
    </city>
  </country>
</countries>

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:template match="/">
      <countries>
         <xsl:for-each select="//country">
            <xsl:sort select="@population" order="descending" data-type="number" />
            <country>
               <name>
                  <xsl:value-of select="@name" />
               </name>
               <population>
                  <xsl:value-of select="@population" />
               </population>
               <area>
                  <xsl:value-of select="@area" />
               </area>
               <cities>
                  <xsl:for-each select="city">
                     <xsl:sort select="population" order="descending" />
                     <city>
                        <name>
                           <xsl:value-of select="name" />
                        </name>
                        <population>
                           <xsl:value-of select="population" />
                        </population>
                     </city>
                  </xsl:for-each>
               </cities>
            </country>
         </xsl:for-each>
      </countries>
   </xsl:template>
</xsl:stylesheet>

下面是在运行eclipse中提到的错误时得到的错误。

"ERROR:  'Could not compile stylesheet'
FATAL ERROR:  '/home/Jon/eclipse-workspace/spark-app/"/home/Jon/XSLT.xsl" (No such file or directory)'
           :/home/Jon/eclipse-workspace/spark-app/"/home/Jon/XSLT.xsl" (No such file or directory)
Exception in thread "main" javax.xml.transform.TransformerConfigurationException: /home/Jon/eclipse-workspace/spark-app/"/home/Jon/XSLT.xsl" (No such file or directory)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:988)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:761)
    at sparkProject.XMLTransformer.xmlProcessor(XMLTransformer.java:19)
    at sparkProject.StartingPoint.main(StartingPoint.java:21)

我有点受不了了,无法弄清楚是什么问题..?

我的XSL或任何代码问题出了点问题。.

请建议..!

1 个答案:

答案 0 :(得分:1)

它报告了一个奇怪的文件名错误

'/home/Jon/eclipse-workspace/spark-app/"/home/Jon/XSLT.xsl"

,这向我强烈暗示,提供给xmlProcessor方法的路径与您认为的不一样;要么,要么正在针对一些奇怪的基本目录进行解析。

相关问题