Populating an RSS feed with our database content? - asp-classic

I have a question about creation of an RSS feed.
I have read a few different bits of information on creation of an RSS feed and this, on the whole doesn't appear too difficult.
What I'm struggling to understand though is how to automatically populate the RSS feed xml file?
Here is our basic .xml file...
<?xml version='1.0' encoding='utf-8'?>
<rss version='2.0'>
<channel>
<title>Channel Title</title>
<description>Description of Channel</description>
<link>http://www.test.com</link>
<item>
<title>Item Title</title>
<description>Updated on: 5/20/2012</description>
<link>http://www.test.com/itempage.asp?itemID=1</link>
</item>
</channel>
</rss>
We can, obviously, manually enter each item but I want this to automatically select the latest items from our database. In this case, news items.

you can use the Msxml2.DOMDocument.6.0 object. have a look here for MSXML DOM Reference.
with that object you can create xml nodes and then save the xml document...
example:
set xml = server.createobject("Msxml2.DOMDocument.6.0")
set rssNode = xml.createElement("rss")
rssNode.appendChild(xml.createTextNode(""))
rssNode.setAttribute "version", "2.0"

Related

Saxon HE 9.7 XQuery results and existing document

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

Create XML doc with Linq to XML

I have an ASP .Net site where i am attempting to create a page to allow the user to create an XML document. New with XML so I'm not sure if im missing something here but all i am trying to do is create an XML document using Linq to XML.
I saw this example as part of my research
Dim xmlDoc As XDocument = _
<?xml version="1.0" encoding="utf-16"?>
<!-- This is comment by you -->
<Employees>
<Employee id="EMP001">
<name>Wriju</name>
<![CDATA[ ~~~~~~~...~~~~~~~~]]>
</Employee>
</Employees>
xmlDoc.Save("somepath\somefile.xml")
but i just dont seem to be able to get it working in VS 2010 or VS 2013. As soon as i enter <?xml and enter to the next line i just get errors that the syntax is incorrect. Reading further i have seen some C# examples but they dont seem to be "readable" in VB .Net.
I've tried using online converters but again no luck in that either. Could anyone guide me or point me to the right direction to create an XML document with elements etc using Linq to XML? I think i can use XDocument but i'm a little confused if that would allow me to use Linq to XML or if this is an old concept.
Thanks
Use this
Dim xmlDoc As XDocument = <?xml version="1.0" encoding="utf-16"?>
<!-- This is comment by you -->
<Employees>
<Employee id="EMP001">
<name>Wriju</name>
<![CDATA[ ~~~~~~~...~~~~~~~~]]>
</Employee>
</Employees>
'it assume that /youfolder is in the root and it has read and write permission
xmlDoc.Save(Server.MapPath("/yourfolder/somefile.xml"))

Translation file still working after modifying the source?

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

css select xml value and print

<?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/

Reference to undeclared namespace prefix when parsing MSXML

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.

Resources