开发者

Can I return a whole XML document using XPath in javascript?

开发者 https://www.devze.com 2023-01-23 00:54 出处:网络
I am using Javascript, and I want to return an XML document with the results I want. For example, the XML document can be

I am using Javascript, and I want to return an XML document with the results I want.

For example, the XML document can be <books> <book> <title> Introduction to XML </title> </book> <book> <title> Introduction to Javascript </title> </book> </books>

and using this XPath expression: //book[./title="Introduction to XML"] I want to get a document like that <boo开发者_运维问答ks> <book> <title> Introduction to XML </title> </book> </books>

I know XQuery sounds like the solution here, but I am using Javascript, and there are no built-in implementations, as far as I know, for XQuery in Javascript.

I want to return the ancestors along with the thing I need. But it should also work if I get several results.. is this doable using XPath?


XPath cannot change the source XML document and cannot produce a new XML document.

Therefore, some other technology has to be used for this. XSLT is a language especially designed for wxpressing such transformations.

In most browsers you can process an XML document with an XSLT stylesheet identified by an associating <?xml-stylesheet ?> PI (processong instruction).

Most browsers also offer some way of initiating an XSLT transformation in Javascript -- read your browser documentation.

The XSLT transformation itself is very simple:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pTitleWanted" select="' Introduction to XML '"/>

 <xsl:template match="node()|@*" name="identity">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="books">
  <xsl:copy>
    <xsl:apply-templates select="book[title = $pTitleWanted]"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied to the provided XML document:

<books>
    <book>
        <title> Introduction to XML </title>
    </book>
    <book>
        <title> Introduction to Javascript </title>
    </book>
</books>

the wanted, correct result is produced:

<books>
   <book>
      <title> Introduction to XML </title>
   </book>
</books>


Browser JavaScript Environment comes with default Xml Document Object Model Parser.

You can take advantage of that control and parse it as Dom Parsing Style.

enter code here
var _xml = "<books> <book> <title> Introduction to XML </title> </book> </books>"
parser=new DOMParser();
xmlDoc=parser.parseFromString(_xml,"text/xml");
//Use any type of DOM API to parse
xmlDoc.childNodes()...... 

This code is for FireFox Browser. You can do the same thing in IE using their ActiveXControls as like XmlHttp way.


In general, no you can't do that with XPath on its own - it's only an expression language. XPath cannot create nodes it can only select them. You can (as you have) select a set of nodes but you can't then wrap them up in other nodes.

You certainly could vary @kadalmittai's suggestion and use the DOM API to select your nodes, create a new document and clone your selected nodes into it but that is fairly heavyweight given your fairly simple requirement.

XSLT might be a better approach here but I don't know enough about browser technologies to tell you how to use it in the browser.

0

精彩评论

暂无评论...
验证码 换一张
取 消