How can i modify xml-stylesheet attribute value in

2019-08-10 22:27发布

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet href="Sample.xsl" type="text/xsl"?>
<MyDoc>.....</MyDoc>

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";

Also using Document only gives elements starting at MyDoc

NodeList list = taggedC32Doc.getElementsByTagName("*");

Is there any way i can do this?

1条回答
冷血范
2楼-- · 2019-08-10 23:03

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)

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.

查看更多
登录 后发表回答