我如何在java中修改xml-stylesheet属性值 [英] How can i modify xml-stylesheet attribute value in java
问题描述
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet href="Sample.xsl" type="text/xsl"?>
<MyDoc>.....</MyDoc>
我想将属性 href 的值修改为MyDoc.xsl".我曾尝试使用 XPath,但它什么也没返回:
I want to modify the attribute href's value to 'MyDoc.xsl'. I have tried using XPath but it returns nothing:
//xml-stylesheet[contains(text(), 'Sample.xsl')]/@href";
同样使用 Document 只给出从 MyDoc 开始的元素
Also using Document only gives elements starting at MyDoc
NodeList list = taggedC32Doc.getElementsByTagName("*");
有什么办法可以做到吗?
Is there any way i can do this?
推荐答案
您要更改的行是一个处理指令,而不是一个元素,因此您尝试将其作为元素查找都不会奏效.试试
The line you want to change is a Processing Instruction, not an Element, so neither of your attempts to find it as an element will work. Try
/processing-instruction(xml-stylesheet)
然后您可以获取该节点的数据,即 href="Sample.xsl" type="text/xsl"
.执行适当的字符串操作以查找和更改该字符串中的 href 伪属性——抱歉,大多数 XML API 不提供任何帮助,因为就 XML 而言,PI 的数据是未格式化的字符串,即使它的结构通常类似于属性——并将新数据设置回 ProcessingInstruction 节点.
You can then get that node's data, which will be href="Sample.xsl" type="text/xsl"
. Perform the appropriate string manipulation to find and change the href pseudo-attribute in that string -- sorry, most XML APIs don't provide any assistance in doing so, because as far as XML is concerned the PI's data is an unformatted string even though it's usually structured to resemble attributes -- and set the new data back into the ProcessingInstruction node.
这篇关于我如何在java中修改xml-stylesheet属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!