首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用XSL可以显示某些特殊字符的ASCII值。

使用XSL可以显示某些特殊字符的ASCII值。
EN

Stack Overflow用户
提问于 2022-01-14 14:07:49
回答 2查看 194关注 0票数 0

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

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

代码语言:javascript
运行
复制
        <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>

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

代码语言:javascript
运行
复制
        <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是定义

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

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

代码语言:javascript
运行
复制
<?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;        
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-15 08:00:13

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

当然,如果您有一个文件,在StreamSource上使用FileInputStream,那么所有猜测编码和手工解码的尝试都不是必要的,而且容易出错,XML解析器通常很擅长根据FileInputStream声明和解码来检测编码。

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

票数 0
EN

Stack Overflow用户

发布于 2022-01-14 14:14:38

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

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70711843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档