热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在使用XSLT转换XML时保存实体引用?-PreservingentityreferenceswhentransformingXMLwithXSLT?

HowcanIpreserveentityreferenceswhentransformingXMLwithXSLT(2.0)?Withalloftheprocesso

How can I preserve entity references when transforming XML with XSLT (2.0)? With all of the processors I've tried, the entity gets resolved by default. I can use xsl:character-map to handle the character entities, but what about text entities?

在使用XSLT(2.0)转换XML时,如何保存实体引用?使用我尝试过的所有处理器,实体将在默认情况下得到解析。我可以使用xsl:character-map来处理字符实体,但是文本实体呢?

For example, this XML:

例如,这个XML:



]>

  Hello &so;!
  &question;

transformed with the following XSLT:

使用以下XSLT进行转换:


  
  

  
    
      
    
  


produces the following output:

产生以下输出:


   Hello stackoverflow!
   How can I preserve the entity reference when transforming with XSLT??

The output should look like the input (minus the doctype declaration for now):

输出应该类似于输入(现在减去doctype声明):


  Hello &so;!
  &question;

I'm hoping that I don't have to pre-process the input by replacing all ampersands with & (like &question;) and then post-process the output by replacing all & with &.

我希望我不需要用& & &取代所有的符号来预处理输入。(像问题一样),然后用替换掉所有的输出来处理后的输出。&。

Maybe this is processor specific? I'm using Saxon 9.

也许这是处理器特有的?我使用Saxon 9。

Thanks!

谢谢!

5 个解决方案

#1


4  

If you know what entities will be used and how they are defined, you can do the following (quite primitive and error-prone, but still better than nothing):

如果您知道将使用什么实体以及如何定义它们,您可以执行以下操作(非常原始且容易出错,但仍然比什么都不做要好):


 

 
  
 

 

 

 
  
   
  
 

 
  
 ]>
]]>
  

  
 

 
  
 

 
  
  
  
  

  
 

when applied on the provided XML document:

当应用于提供的XML文档时:


 ]>

    Hello &so;!
    &question;

the wanted result is produced:

所需结果如下:


 ]>

  
      Hello &so;!
      &question;

Do note:

注意:

  1. The special (RegEx) characters in the replacements must be escaped.

    必须转义替换中的特殊字符(RegEx)。

  2. We needed to resolve to DOE, which isn't recommended, because it violates the principles of the XSLT architecture and processing model -- in other words this solution is a nasty hack.

    我们需要解析DOE,这是不推荐的,因为它违反了XSLT体系结构和处理模型的原则——换句话说,这个解决方案是一个令人讨厌的黑客。

#2


3  

This can be an especially troublesome issue if you are using something like S1000D. It uses entities and @boardno attributes to link to figures. It's a throwback to its SGML roots.

如果您使用的是S1000D之类的东西,这可能是一个特别麻烦的问题。它使用实体和@boardno属性链接图。这是对SGML根源的回归。

Because this automatic entity expanding behavior, which is correct but undesireable, I often have to drop back to tools like sed, awk and batch scripts to manage certain data analysis tasks when using S1000D as input.

由于这种自动的实体扩展行为是正确的,但是不受欢迎,所以在使用S1000D作为输入时,我经常不得不使用sed、awk和批处理脚本等工具来管理某些数据分析任务。

IMHO, this would be a great change proposal to one of the upcoming XSLT specifications that a compliant processor accept a runtime parameter that can turn on and off entitiy expansions.

IMHO,这将是对即将发布的XSLT规范的一个重大修改,该规范要求兼容的处理器接受运行时参数,该参数可以打开和关闭实体扩展。

#3


1  

If you use a Java implementation of an XSLT 2.0 processor (like Saxon 9 Java) you might want to check whether http://andrewjwelch.com/lexev/ helps out, you can preprocess your XML with entity and character references that way to get them marked up as XML elements you can then transform as necessary.

如果您使用XSLT 2.0处理器的Java实现(如Saxon 9 Java),您可能想检查http://andrewjwelch.com/lexev/是否有帮助,您可以使用实体和字符引用对XML进行预处理,以便将它们标记为XML元素,然后您可以根据需要进行转换。

#4


1  

I use this solution and it works well :

我用这个方法,效果很好:




    
  

#5


0  

You can keep EntityReference nodes in the document by using a DOM LS parser with "entities" parameter set to true. http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/DOMConfiguration.html

您可以通过使用“entity”参数设置为true的DOM LS解析器来保持文档中的EntityReference节点。http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/DOMConfiguration.html

The specification says the default value is true but depending on the parser, it could be false, be aware of that.

规范说默认值是true,但是根据解析器的不同,它可能是false,请注意这一点。

To load Xerces :

加载Xerces:

DOMImplementationLS domImpl = new org.apache.xerces.dom.CoreDOMImplementationImpl();

You can use registry as below too but personnaly, I would rather hardcode the implementation I want as above:

你也可以使用注册表如下,但就个人而言,我宁愿硬编码实现我想要的如上:

DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domImpl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0"); 

Then, to load your document :

然后,加载您的文档:

// XML parser with XSD schema 
LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema");
DOMConfiguration cOnfig= parser.getDomConfig();
config.setParameter("entities", true);
LSInput input = impl.createLSInput();
Document lDoc = parser.parse(your XML stream);

Then, your XML entities are not expanded in the DOM.

然后,XML实体不会在DOM中展开。

Then, because SAXON does not handle entities not expanded ('Unsupported node type in DOM! 5' error), you can not use net.sf.saxon.xpath.XPathFactoryImpl, you have to set the default XPathFactory of Xerces with XPathFactory.newInstance()

然后,因为SAXON不会处理未展开的实体(“DOM中不支持的节点类型!”5’error),不能使用net.sf.saxon.xpath。XPathFactoryImpl,您必须使用XPathFactory. newinstance()设置Xerces的默认XPathFactory。


推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
author-avatar
JSHGDF5649
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有