1
<?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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The processing-instruction() node test is also available in XPath 1.0.
... Yup. I checked the REC because I wasn't certain (I've been away from XSLT for a while), and somehow missed it. Thanks for keeping me honest.
(Rechecking... It's there, but not as well documented as I'd like. Haven't yet checked whether there's been an erratum filed to suggest this be fixed.)
XPath 1.0 generally isn't as well documented as some people would like. No one is going to fix that.
Yeah, I should have checked your book (free unsolicited plug) ... I really wish someone had the time/energy/determination to do equivalents of the Annotated XML Rec for other W3C specs, or even to update that one to XML 1.1. Oh well. Digression, apologies to all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.