【问题标题】:Using XSL it can show ASCII value of some special character使用 XSL 它可以显示一些特殊字符的 ASCII 值
【发布时间】:2022-01-14 14:07:49
【问题描述】:

我在使用 XSL 转换 XML 时遇到了一些问题,然后它没有解析项目符号,它给了我一些 ASCII 字符,如下所示。

这是将复杂的 xml 转换为简化的 XML 的 XSL。

        <xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xpath-default-namespace="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/document">
            <document>
                <xsl:for-each select="page">
                    <page>
                        <xsl:for-each select="block">
                            <block blockType="{@blockType}">
                               <xsl:for-each select="text">
                                   <text>
                                        <xsl:for-each select="par">
                                            <paragraph>
                                                <line>
                                                    <xsl:value-of select="line"/>
                                                </line>
                                            </paragraph>
                                        </xsl:for-each>
                                    </text>
                                </xsl:for-each>
                            </block>
                        </xsl:for-each>
                    </page>
                </xsl:for-each>
            </document>
        </xsl:template>
        </xsl:stylesheet>
        

的开头,当我们使用 xsl 从复杂的 xml 转换 xml 时,它需要显示项目符号 bt 它显示一些 ascii 值。我使用 saxon 转换使用 xsl 样式表语言转换 xml

        <paragraph>
                       <line>?¢â?¬?¢ If you have to take a picture of a document in poor lighting and need the flash, try to use the flash from 20 inches away and try to find additional light sources.</line>
                    </paragraph>
    

XSL 是一系列用于定义 XML 文档转换和表示的建议。 XSLT 样式表通过描述如何将类的实例转换为使用格式化词汇表的 XML 文档来指定 XML 文档类的表示形式,或者可扩展标记语言 (XML) 是一种标记语言,它定义了一组编码规则格式为人类可读和机器可读的文档

这是使用 XML 样式表语言转换的 XML。当我使用在线 XSL 转换时,它给了我一个正确的答案,但使用撒克逊转换不会给我确切的结果。我不知道我在哪里做错了为什么它没有给我正确的结果。其背后的问题是转换还是 XSL?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document xmlns="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" version="1.0" producer="ABBYY FineReader Engine 12" pagesCount="2" languages="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml">
<page width="2550" height="3300" resolution="300" originalCoords="1">
<block blockType="Text" blockName="" l="273" t="1721" r="2281" b="2618"><region><rect l="273" t="1721" r="2281" b="2618"/></region>
<text>
<par leftIndent="3600" startIndent="-1800" lineSpacing="1152" isListItem="1" lstLvl="0">
<line baseline="2232" l="355" t="2201" r="2275" b="2240"><formatting lang="EnglishUnitedStates">• Use the white balance feature. If your camera has manual white balance, use a white sheet of paper</formatting></line>
<line baseline="2280" l="429" t="2249" r="2209" b="2288"><formatting lang="EnglishUnitedStates">to set white balance. Otherwise, select the appropriate balance mode for your lighting conditions.</formatting></line></par>
<par startIndent="1800" lineSpacing="1152" isListItem="1" lstLvl="0">
<line baseline="2331" l="355" t="2300" r="1416" b="2339"><formatting lang="EnglishUnitedStates">• Enable the anti-shake setting: otherwise, use a tripod.</formatting></line></par>
<par lineSpacing="1152">
<line baseline="2403" l="282" t="2373" r="759" b="2412"><formatting lang="EnglishUnitedStates">In poor lighting conditions:</formatting></line></par>
<par startIndent="1800" lineSpacing="1152" isListItem="1" lstLvl="0">
<line baseline="2454" l="355" t="2423" r="1930" b="2462"><formatting lang="EnglishUnitedStates">• Auto focus may function incorrectly: therefore, you should switch to manual focus.</formatting></line></par>
<par leftIndent="3600" startIndent="-1800" lineSpacing="1152" isListItem="1" lstLvl="0">
<line baseline="2505" l="355" t="2474" r="2154" b="2513"><formatting lang="EnglishUnitedStates">• Use the maximum aperture allowed by the camera (2.3 or 4.5). (In bright daylight, use smaller</formatting></line>
<line baseline="2553" l="430" t="2522" r="1245" b="2561"><formatting lang="EnglishUnitedStates">apertures: this will produce sharper images).</formatting></line></par>
<par startIndent="1800" lineSpacing="1152" isListItem="1" lstLvl="0">
<line baseline="2603" l="355" t="2572" r="2121" b="2612"><formatting lang="EnglishUnitedStates">• If your camera gives you more than one choice of ISO speed, select the highest ISO setting.</formatting></line></par>
</text>
</block>
<block blockType="Picture" blockName="" l="315" t="1349" r="697" b="1693"><region><rect l="315" t="1349" r="697" b="1693"/></region>
</block>
<block blockType="Text" blockName="" l="1270" t="3021" r="1304" b="3067"><region><rect l="1270" t="3021" r="1304" b="3067"/></region>
<text>
<par lineSpacing="1380">
<line baseline="3061" l="1276" t="3027" r="1297" b="3061"><formatting lang="EnglishUnitedStates">2</formatting></line></par>
</text>
</block>
</page>
</document>

Here is the saxon parser transformation that used to translate it
public static String saxonTransform(String xml, String xsl) throws TransformerException, FileNotFoundException {
        TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
        f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
        try {
            StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xsl.getBytes(Charset.forName("UTF-8"))));
            Transformer t = f.newTransformer(xsrc);
            StreamSource src = new StreamSource(new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))));
            StreamResult res = new StreamResult(new ByteArrayOutputStream());
            t.transform(src, res);
            return res.getOutputStream().toString();
        } catch (Exception e) {
            logger.warn(e.getMessage());
        }
        return null;
    }

here is the way that convert the file into XML
 public  String  FileToXmlString( String path){
        String str="";
        String str1="";
        try {
            str=new String(Files.readAllBytes(Paths.get(path)));
            str1=str.substring(3);
            }
            catch (IOException e) {
                logger.error(e.getMessage());
            }
        return str1;        
    }

【问题讨论】:

  • 您使用哪个工具或哪个 API 来解析 XML 和/或运行 XSLT?输入 XML 具有哪种编码? XSLT的输出有哪些编码,怎么渲染的,看看?
  • 嗨 Martin,我正在使用 Saxon 解析器用于通过 XSL 解析 XML
  • 那么,请告诉我们您使用的是哪个版本的 Saxon,并向我们展示您是如何使用它的,您是否以及如何从命令行运行它,或者您编写了哪些 Java 或 C# 代码来运行它。不知何故,您的输入解析出错了,但我们无法仅根据 XML 和/或 XSLT 的一些代码示例来判断,您需要解释您的具体操作。
  • 嗨,马丁,我也添加了转换 XML 的转换代码。

标签: java xml xslt xsd


【解决方案1】:

如果您有一个带有 Unicode 字符的 Java 字符串,那么将它们提供给 XML 解析器/JAXP 转换器的正确方法是使用 StreamSource 而不是 StringReader,即 StreamSource src = new StreamSource(new StringReader(xml));。 您还没有展示如何构造字符串 xml,但是一旦您有了带有字符的字符串,就可以使用 StringReader。

当然,如果你有一个文件,在 FileInputStream 上使用 StreamSource,所有猜测编码和手动解码的尝试都是不必要的并且容易出错,XML 解析器通常非常擅长检测基于编码的根据需要进行 XML 声明和解码。

由于您还需要一个字符串作为转换结果,我还建议使用 StreamResult 而不是 StringWriter。

【讨论】:

  • 嗨 Martin,当我将文件转换为代码末尾显示的 XML 时,我遇到了问题。也请在那里看看。如果你知道任何替代方案,请告诉我。
  • 我建议放弃所有代码以将文件读入字符串,然后通过使用 StreamSource 而不是 FileInputStream 而不是 File 将该作业交给 XML 解析器。这样,XML 解析器将根据 XML 声明和/或 BOM 进行解码,并且比任何尝试依赖基于特定字符集或代码页的特定编码/解码来将字节转换为字符串的尝试做得更好。
  • 知道了,谢谢你,马丁
【解决方案2】:

问题是输入文件没有按照 XML 解析器认为的方式编码,因此 XML 解析器错误地解码了字符。检查输入的 XML 文件是否有声明编码的 XML 声明,并检查行首的项目符号字符是否实际上按照应有的方式编码。

像 Oxygen 这样好的 XML 编辑器应该可以帮助您解决这个问题。

当然,一旦你确切地发现编码问题是什么,你需要调查它是如何发生的,并确保它不会再次发生。

(顺便说一下,它是“ascii”而不是“ascaii”,而且您看到的字符是非ASCII字符。当您处理字符编码问题时,您需要精确。)

【讨论】:

  • 嗨,Michael,感谢您的即时回复,我也在更新我的 XML。当我使用在线转换时,它给了我正确的答案,但使用撒克逊语给了我错误的答案。你能指导我问题出在哪里吗?
  • 我不能告诉你更多,就像我已经告诉你的那样:输入文件被错误地解码(由 XML 解析器),这可能意味着 XML 声明中命名的编码是不是实际的编码。
猜你喜欢
  • 1970-01-01
  • 2013-02-23
  • 2015-06-11
  • 2020-08-28
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多