<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="tibu.css"?>
<THREADS>
<thread address="0011">
<sms msgBox="inbox" date="2012-10-30T06:49:36.529Z" locked="false" seen="true" read="true" serviceCenter="121212" address="0011" encoding="plain">aaaa</sms>
</thread>
<thread address="0123">
<sms msgBox="inbox" date="2012-10-30T06:49:36.529Z" locked="false" seen="true" read="true" serviceCenter="121212" address="0123" encoding="plain">Bugbug</sms>
</thread>
</THREADS>
Is it possible to select the date value and then print it?
If "yes" how can i do it?
I don't think CSS can do this, or if it can it isn't the best tool for the job
Take a look at XSL Transformations
XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.
Very good resource and tutorials
http://www.w3schools.com/xsl/
Related
I am new to Saxon.
In my java application, I have a requirement that I need to XQuery an existing dom4j document. The XQuery is to order few elements in an descending order by serialNo:
<?xml version="1.0" encoding="UTF-8"?>
<dataOfBooks:DataOfBooks xmlns:dataOfBooks="DataOfBooks">
<Id>ID123</Id>
<books>
<book>
<name>ccc</name>
<serialNo>77</serialNo>
</book>
<book>
<name>aaa</name>
<serialNo>99</serialNo>
</book>
</books>
</dataOfBooks:DataOfBooks>
Once I get the XQuery results, I need to add those back to the above existing document. I tried using net.sf.saxon.s9api. I was able to get the XQuery results back as below:
<?xml version="1.0" encoding="UTF-8"?>
<result:sequence
xmlns:result="http://saxon.sf.net/xquery-results"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<result:element>
<book xmlns:data="dataOfBooks">
<name>aaa</name>
<serialNo>99</serialNo>
</book>
<book xmlns:data="dataOfBooks">
<name>aaa</name>
<serialNo>77</serialNo>
</book>
</result:element>
</result:sequence>
But I have two issues. 1) the result has namespaces and extra stuff that I do not want. 2) It is not very clear to me as which Saxon API to use to add the XQuery results to the existing document. So that the resultant document looks as:
<?xml version="1.0" encoding="UTF-8"?>
<dataOfBooks:DataOfBooks xmlns:dataOfBooks="DataOfBooks">
<Id>ID123</Id>
<books>
<book>
<name>aaa</name>
<serialNo>99</serialNo>
</book>
<book>
<name>ccc</name>
<serialNo>77</serialNo>
</book>
</books>
</dataOfBooks:DataOfBooks>
One more question - I tried using dynamicContext and treeinfo classes since I though the usage of treeinfo API might be more optimal, but no luck. If you think, usage of TreeInfo API is efficient, I really appreciate a code example for my requirement. Your help is much appreciated.
Thanks in advance for your time and interest.
The fact that your XQuery code is producing unwanted namespaces is because your query is wrong, but we can't tell you how it is wrong unless you show us the code.
The result:sequence in your output suggests that you have somehow contrived to ask for output in "wrapped" format, which suggests some kind of misuse of Saxon APIs. Again, without seeing your code, we can't tell you exactly what you have done wrong.
To make small changes to an existing document, leaving the rest unchanged, I would normally recommend XSLT over XQuery. In XSLT 3.0, you can sort the books by name using the following stylesheet:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="books">
<xsl:copy>
<xsl:perform-sort select="book">
<xsl:sort select="name"/>
</xsl:perform-sort>
</xsl:copy>
</xsl:template>
</xsl:transform>
In both XQuery and XSLT, the result of your query/transformation is a new document, which you can use in place of the original. If you want to make in-situ updates to an existing document, you can do this using XQuery Update; however Saxon does not support XQuery Update against documents in DOM4J format.
Saxon does allow you to capture the result of a query or transformation as a DOM4J Document, and you could use DOM4J APIs to graft this document (or rather, its outermost element) back into the original DOM4J document.
Later
You have now provided your code (you should have provided it as an edit to the original question, not as an answer).
I guess your DOMWriter is the DOM4J class of that name, which like much of DOM4J is rather badly documented. But I think it is copying the DOM4J tree to a DOM tree, which you definitely don't want to do. If you really want to copy the tree to make it convenient for Saxon, you should copy it to a Saxon tree, but for this use case it's best to leave it in DOM4J form. Use
DocumentBuilder builder = processor.newDocumentBuilder();
XdmNode inDoc = builder.wrap(dom4jdoc);
When you run your query, the resulting XdmValue will now be a sequence of XdmNode objects, each of which is a wrapper around a DOM4J Element node. These element nodes are still attached to the original DOM4J tree, and they still have their original namespaces. There is no need to serialize the result to lexical XML.
You can copy the result to a List value by writing
List<Element> sortedNodes = new ArrayList<Element>();
for (XdmItem item : result) {
sortedNodes.add(((Element)((XdmNode)item).getExternalNode()));
}
and then (if I read the DOM4J documentation correctly) you can replace the content of the containing books element with
Element books = (Element)sortedNodes.get(0).getParent();
List booksContent = books.elements();
booksContent.clear();
booksContent.addAll(sortedBooks);
A part of my .ts file, created with pylupdate4, looks like:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS><TS version="2.0">
<context>
<name>Main</name>
<message>
<location filename="Main.py" line="2369"/>
<source>Translate me to Ukululu</source>
<translation type="unfinished"></translation>
</message>
Two questions:
1: The translation comes between the <translation>-tags, right?
2: The translation seems to need the line in the main file where the translation is to be inserted ("line=2369"). But everytime I modify the code, the lines change. Hence, do I have to create a new translation file, where I have to manually insert all the (unchanged) translations between the tags again? This seems like a crazy amount of work. Correct me if I am wrong or please explain, how it works (better)!
You are right. The original text comes in the tag <source> while the translation comes in the tag <translation>. When the translation is finished the attribute type="unfinished" will also be removed.
Usually you don't edit ts files manually but you use the QtLinguist tool. I never edit them by hand, since QtLinguist is very easy to use...
In any case, when you change the source code, you simply run
pylupdate4 your_project.pro
This will update all the references to the lines in the ts file, and it will keep the translation already finished. It is smart enough to update the translations even if you move them to a different source file.
You can get further information here
http://pyqt.sourceforge.net/Docs/PyQt4/i18n.html
http://qt-project.org/doc/qt-4.8/linguist-manager.html
Hope this helps
During MSDEPLOY.EXE deployment, I am trying to squirt a parameter into an XML configuration file but the configuration values are stored in CDATA elements. Here are the contents of the file, called paths.xml:
<?xml version="1.0" encoding="UTF-8"?>
<course>
<questionnaires><![CDATA[https://www.site.com/somepage.asp]]></questionnaires>
</course>
I need to transform that URL into something different, but I can't figure out the correct XPATH and syntax for my parameters.xml file, here is what I've got now:
<?xml version="1.0" encoding="utf-8"?>
<parameters>
<parameter name="QuestPath" description="Questionnaires path" defaultValue="<questionnaires><![CDATA[https://www.foo.com/somepage.asp]]></questionnaires>" tags="">
<parameterEntry kind="XmlFile" scope="paths.xml$" match="/course/questionnaires" />
</parameter>
</parameters>
I had very little luck referencing the CDATA element to replace it, so you can see I'm now trying to replace the entire questionnaire element including its CDATA contents. I had to do some escaping of the embedded angle-brackets so parameters.xml wasn't rejected due to invalid XML format.
Now, the resultant paths.xml ends up like:
<?xml version="1.0" encoding="UTF-8"?>
<course>
<questionnaires>https://www.foo.com/somepage.asp</questionnaires>
</course>
So, something has resolved the CDATA element down to its contents only, and CDATA no longer appears in paths.xml which I assume will cause the program that reads it to fail. Help!
Okay - I found a much simpler solution. In conjunction with the developer involved in housing this config XML file on our server, we changed it so the elements just contain the raw URLs, completely dispensing with the CDATA structures.
After testing that the consuming application still appeared to work fine, we agreed they were not needed and therefore I was able to do normal XmlFile replacements on nodes referenced like:
...match="/course/questionnaires/text()"
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.
My incoming XML has the following
<ROOT-PARENT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ASR_V3.0.1_proposed.xsd">
I am using a Inline XSLT in Scripting Functoid in BizTalk 2010
I want to navigate one of the child nodes but I am unable to get the values. Do i need to
My XSLT looks like
<EXTERNALTAG xsi:noNamespaceSchemaLocation="ASR_V3.0.1_proposed.xsd">
<xsl:for-each select ="//MILESTONES/MILESTONE">
<P><xsl:value-of select="."/></P>
</xsl:for-each>
</EXTERNALTAG>
The above doesn't seem to work. Any idea what i need to do in order to take care of the
xsi:noNamespaceSchemaLocation
Thanks in advance
Karthik
OK. If my XSLT looks like this
<EXTERNALTAG> <xsl:for-each select ="//MILESTONES/MILESTONE"> <P><xsl:value-of select="."/></P> </xsl:for-each> </EXTERNALTAG>
everything is working fine
Thanks