I would like to get all the ids of xml nodes via xquery
I am trying this line but does not work(returns empty characters)
xquery doc('MYdb/MY.xml')/ROOT/*/string(#ID)
the xml is like this
ROOT
<NODE>
<ID>CP1</ID>
<..>
</NODE>
<NODE>
<ID>CP2</ID>
<...>
</NODE>
...
</ROOT>
I want to receive a string like this CP1,CP2,CP3....
That would be
fn:doc("MYdb/MY.xml")//ID/fn:string()
because your ids are not attributes but plain elements.
The // prefix will get all ID elements from every level of your document tree
HTH
Peter
Related
I have to make multiple calls to a Xquery server in a single request (to enable a transaction behavior). It's something like this
declare namespace foo = "ns.bar.foo";
( foo:call("toto"), foo:call("tata"), foo:call("titi"))
But if I do that, the server returns
<?xml version="1.0" encoding="UTF-8"?>
<call-result> 1 </call-result>
<call-result> 2 </call-result>
<call-result> 3 </call-result>
Which is not a valid XML file because the results are not inside a root tag.
My first try was something like
declare namespace foo = "ns.bar.foo";
<results> {( foo:call("toto"), foo:call("tata"), foo:call("titi"))} </results>
But since foo:call is an updating expression, it's not allowed.The only thing I see is to modify the received XML on the client side but that's really dirty.
I could also add a method on the server side, like a foo:calls, but foo:call already works with sequences as a parameter, and you cannot have sequences of sequences in xQuery. Any suggestion?
I'm trying to understand this xslt.
What does the below xslt command select exactly? what are "following-sibling", "aic" and "pstyle"?
"aic" seems to be a namespace.
What xml input the below xslt work with?
<xsl:stylesheet exclude-result-prefixes="aic"
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aic="http://ns.adobe.com/AdobeInCopy/2.0/" >
<xsl:template match="/">
<xsl:value-of select="following-sibling::aic:pstyle"/>
</xsl:template>
</xsl:stylesheet>
following-sibling::aic:pstyle
following-sibling is the axis, denoting which "direction" to look for nodes, in this case it looks at nodes which are after the current context node in document order but share the same parent as the current node. If you don't specify an axis the default is child, which looks for child nodes of the current context node.
aic:pstyle is a selector that looks for elements whose local name is pstyle and whose namespace URI is http://ns.adobe.com/AdobeInCopy/2.0/ (the one that is mapped to the prefix aic in the stylesheet).
The source XML need not use the same prefix, e.g. the expression would match an element that looks like
<pstyle xmlns="http://ns.adobe.com/AdobeInCopy/2.0/">
or
<foo:pstyle xmlns:foo="http://ns.adobe.com/AdobeInCopy/2.0/">
in the original XML.
As JLRishe points out, this particular XPath will not match anything if the current context is the document node /, for the expression to be meaningful it would have to be executed in a context where the current node is an element (or comment, processing instruction or text node) at least two levels down i.e. a child of the document element or deeper.
<example xmlns="http://ns.adobe.com/AdobeInCopy/2.0/">
<pstyle id="1"/>
<foo/>
<pstyle id="2"/>
<pstyle id="3"/>
</example>
If executed with the foo element as the context node, the expression would select pstyle elements 2 and 3, but not 1.
How do I solve the
Reference to undeclared namespace prefix: '%s'
problem with Microsoft's msxml implementation?
I'm using an XML feed from a government web-site that contains values i need to parse. The xml contains namespaces:
<?xml version="1.0" encoding="ISO-8859-1"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/"
xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3c.org/1999/02/22-rdf-syntax-ns#rdf.xsd">
<item rdf:about="http://www.bankofcanada.ca/stats/rates_rss/STATIC_IEXE0101.xml">
<cb:statistics>
<cb:exchangeRate>
<cb:value decimals="4">1.0351</cb:value>
<cb:baseCurrency>CAD</cb:baseCurrency>
<cb:targetCurrency>USD</cb:targetCurrency>
<cb:rateType>Bank of Canada noon rate</cb:rateType>
<cb:observationPeriod frequency="daily">2011-05-09T12:15:00-04:00</cb:observationPeriod>
</cb:exchangeRate>
</cb:statistics>
</item>
</rdf:RDF>
Running the XPath query:
/rdf:RDF/item/cb:statistics/cb:exchangeRate/cb:targetCurrency
fails with the error:
Reference to undeclared namespace prefix: 'rdf'
Edit:
If i edit the original XML to remove all use of namespaces:
<?xml version="1.0" encoding="ISO-8859-1"?>
<rdf>
<item>
<statistics>
<exchangeRate>
<value decimals="4">1.0351</value>
<baseCurrency>CAD</baseCurrency>
<targetCurrency>USD</targetCurrency>
<rateType>Bank of Canada noon rate</rateType>
<observationPeriod frequency="daily">2011-05-09T12:15:00-04:00</observationPeriod>
</exchangeRate>
</statistics>
</item>
</rdf>
The query /rdf/item/statistics/exchangeRate/baseCurrency doesn't fail, and returns nodes:
<baseCurrency>CAD</baseCurrency>
How do i get Microsoft XML to work with namespaces?
Edit 2
i've tried adding SelectionNamespaces to the DOMDocument object:
doc.setProperty('SelectionNamespaces', 'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"');
Now the xpath query doesn't fail, but it also returns no nodes:
nodes = doc.selectNodes('/rdf:RDF/item/cb:statistics/cb:exchangeRate/cb:targetCurrency');
See also
“undeclared reference to namespace prefix ” error
XMLReader - How to handle undeclared namespace
PRB: Specifying Fully Qualified Element Names in XPath Queries
XPath not working properly.
Using SelectionNamespaces is the correct approach, you are just missing a namespace.
Notice that your XML document explicitly sets the default namespace as follows:
xmlns="http://purl.org/rss/1.0/"
This means that any element without a prefix, such as the item element, is actually in the default namespace. So if you want to select that element with an XPath expression, you must first set an appropriate selection namespace.
To do this, you can change your call to setProperty like so:
doc.setProperty('SelectionNamespaces', 'xmlns:rss="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"');
Here you've assigned the default namespace from the document to the rss: prefix in your XPath expression. With that change in place, the following XPath expression should work correctly:
nodes = doc.selectNodes('/rdf:RDF/rss:item/cb:statistics/cb:exchangeRate/cb:targetCurrency');
It works because it references the item element using the correct namespace. The fact that the prefix differs between the XPath expression and the original document is immaterial. It is the namespace which the prefix is bound to that matters.
doc.setProperty('SelectionNamespaces', 'xmlns:rss="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"');
Dont forget to load the xsd file or schema to the xmldoc object
is the way to go
I dont have enough reputation to comment. But that bit there saved me a lot of time.
Thank you so much
If you are using XMLSerializer and see this error, it is likely that you are running into the IE bug described here:
https://stackoverflow.com/a/11399681
It took me a lot of time to realize that this was happening, so I thought it best to link these two issues.
I have a XMLDataSource somewhat like:
<bookstore>
<author>author1</author>
<publication>publication1</publication>
<book>
<genre>Thriller</genre>
<name>ABC</name>
</book>
<book>
<genre>Romance</genre>
<name>XYZ</name>
</book>
<book>
<genre>Horror</genre>
<name>000</name>
</book>
</bookstore>
I am storing these in a asp:formview. I am able to store author and publication values but not sure how can I store the value of book/name based on some condition? Actually I just want to use condition that I need to store the value of "name" if "genere=Romance". something like this. I tried using XPath expression bookstore/book/genre[. ='Romance'] but not sure how to access the value of tag. Checked the following resource:
http://msdn.microsoft.com/en-us/library/ms256086.aspx
Appreciate if someone can help me.
I tried using XPath expression
bookstore/book/genre[. ='Romance']
but not sure how to access the value
of tag
Almost. This XPath expression:
/bookstore/book[genre='Romance']/name
String value: XYZ
You probably need to add /text() to get the contents of the XML tag instead of just the tag. There is a great XML cheat-sheet here that should help you.
I have created XML file called users.xml
Looks like this:
<Users>
<user>
<uin>"0012345"</uin>
<name>black</name>
<email>"bk#hotemail.com"</email>
<created>"3/02/2010"</created>
</user>
<user>
<uin>"123456780"</uin>
<name>sam</name>
<email>"sam#hotmail.com"</email>
<created>"3/02/2010"</created>
</user>
<user>
<uin>"123456799"</uin>
<name>kblack</name>
<email>"kblack#hotmail.com"</email>
<created>"3/02/2010"</created>
</user>
</Users>
I want to encrypt the element. Using code like
XmlElement uinelement = (XmlElement)xmldoc.SelectSingleNode("Users/user/uin");
...encrypts only first UIN from the user.xml file.
How can I Encrypt all UIN elements?
Thank you
Kanta
It looks like your xml was not correctly encoded, so I can't see your document structure, however I would guess that using the SelectNodes() method of the XmlDocument class instead of SelectSingleNode() would do the trick.
You may also want to look at some of the Linq to XML capabilities if you need to output the transformation to a new XML doc.