I have a valid rdf/xml file and I have to put it in another tag, so on the first level to have only one tag element.
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:rtc_cm="http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/"
xmlns:oslc="http://open-services.net/ns/core#" >
<rdf:Description rdf:about="https://10.0.2.79:9443/ccm/oslc/types/_tsVvMWWwEeWQIIEAtKgWEg/com.ibm.team.apt.workItemType.story">
<rdf:type rdf:resource="http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/Type"/>
<rtc_cm:projectArea rdf:resource="https://10.0.2.79:9443/ccm/oslc/projectareas/_tsVvMWWwEeWQIIEAtKgWEg"/>
<rtc_cm:category rdf:datatype="http://www.w3.org/2001/XMLSchema#string">com.ibm.team.workitem.workItemType.story</rtc_cm:category>
<rtc_cm:iconUrl rdf:resource="https://10.0.2.79:9443/ccm/service/com.ibm.team.workitem.common.internal.model.IImageContentService/processattachment/_tsVvMWWwEeWQIIEAtKgWEg/workitemtype/story.gif"/>
<dcterms:title rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Story</dcterms:title>
<dcterms:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#string">com.ibm.team.apt.workItemType.story</dcterms:identifier>
</rdf:Description>
...
<rdf:Description ...2>
</rdf:Description ...2>
</rdf:RDF>
Here as you see there are can be more than one Description elements.
I want to put all of them in one tag. How to do that? If I try:
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:rtc_cm="http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/"
xmlns:oslc="http://open-services.net/ns/core#" >
<rdf:MyTag rdf:about="https://10.0.2.79:9443/ccm/oslc/types/_tsVvMWWwEeWQIIEAtKgWEg/com.ibm.team.apt.workItemType.story">
<rdf:Description rdf:about="https://10.0.2.79:9443/ccm/oslc/types/_tsVvMWWwEeWQIIEAtKgWEg/com.ibm.team.apt.workItemType.story">
<rdf:type rdf:resource="http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/Type"/>
<rtc_cm:projectArea rdf:resource="https://10.0.2.79:9443/ccm/oslc/projectareas/_tsVvMWWwEeWQIIEAtKgWEg"/>
<rtc_cm:category rdf:datatype="http://www.w3.org/2001/XMLSchema#string">com.ibm.team.workitem.workItemType.story</rtc_cm:category>
<rtc_cm:iconUrl rdf:resource="https://10.0.2.79:9443/ccm/service/com.ibm.team.workitem.common.internal.model.IImageContentService/processattachment/_tsVvMWWwEeWQIIEAtKgWEg/workitemtype/story.gif"/>
<dcterms:title rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Story</dcterms:title>
<dcterms:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#string">com.ibm.team.apt.workItemType.story</dcterms:identifier>
</rdf:Description>
</rdf:MyTag>
</rdf:RDF>
I added MyTag, but an error apperas:
Error: {E205} rdf:Description is not allowed as an element tag here.[Line = 9, Column = 128]
Error: {E201} rdf:about not allowed as attribute here.[Line = 9, Column = 128]
Error: {E201} rdf:resource not allowed as attribute here.[Line = 10, Column = 79]
Error: {E201} Multiple children of property element[Line = 11, Column = 110]
Error: {E201} rdf:resource not allowed as attribute here.[Line = 11, Column = 110]
......
Warning: {W113} rdf:MyTag is not a recognized RDF property or type.[Line = 8, Column = 122]
I use for validation:
http://www.w3.org/RDF/Validator/
I do something stupid I thing. Probably in the http://www.w3.org/RDF/Validator/ are defined other tags than MyProp, but if I open that link I can not see the valid tags? How to fix the error?
I want to put all of them in one tag
Why?
RDF/XML is a serialisation of a RDF Graph in XML, you are constrained by the rules of RDF/XML and you really should not care what the XML looks like.
If you are working with a system that does care then that is a bug/poor design in the system you are working with and the system should change not the RDF. This of course assumes that your RDF/XML expresses the appropriate RDF graph that the system wishes to consume.
Any manipulation of RDF/XML (or any other RDF serialisation) should be done using an appropriate API or Toolkit that will understand and enforce the rules for you.
For anything beyond trivial examples you should really never be editing RDF by hand.
As #RobV indicated, technically it's possible to do this, but it is fundamentally problematic that the tool you're using requires this, and likely indicates deeper problems - so I doubt that fixing this will help you all that much: any fix that results in syntactically legal RDF/XML is likely to still be impossible to process by this tool.
However, here goes: in RDF/XML, each Description element captures the description (in terms of its properties) for a particular resource. The resource described is the one identified in the rdf:about attribute.
So:
<rdf:Description rdf:about="http://example.org/person1">
<rdfs:label>John</rdfs:label>
</rdf:Description>
is a description of a single resource http://example.org/person1, and its property rdfs:label, which has the value "John". In RDF-terms: we have defined a single triple (or statement):
<http://example.org/person1> rdfs:label "John" .
Your RDF file contains more than one description. Although you haven't shown the rdf:about attributes of any of the other Description elemements, I assume they are different - which means they are descriptions of different things :
<rdf:Description rdf:about="http://example.org/person1">
<rdfs:label>John</rdfs:label>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/person2">
<rdfs:label>Paul</rdfs:label>
</rdf:Description>
which corresponds to the following RDF statements:
<http://example.org/person1> rdfs:label "John" .
<http://example.org/person2> rdfs:label "Paul" .
We could of course put everything in a single Description element, but then what should the value of its rdf:about attribute become? If we picked one at random we'd get something like this:
<rdf:Description rdf:about="http://example.org/person1">
<rdfs:label>John</rdfs:label>
<rdfs:label>Paul</rdfs:label>
</rdf:Description>
While doing this leads to syntactically correct RDF/XML, it corresponds to the following RDF model:
<http://example.org/person1> rdfs:label "John" .
<http://example.org/person1> rdfs:label "Paul" .
This is clearly undesirable, since it is factually wrong (there is not just one person called both "John" and "Paul", there's two separate persons, one named "John", the other named "Paul"). In other words, it fixes the syntax problem, but messes up the actual meaning of your data.
The first element underneath the rdf:RDF element in RDF/XML is expected to be either a rdf:Description element, or a different element which indicates a class name (this is a shorthand for writing down a description of a resource and assigning it a class by adding a property). Let's forget about that shorthand notation for a bit and just focus on rdf:Description. Clearly, we can't just have something like this:
<rdf:Description>
<rdf:Description rdf:about="http://example.org/person1">
<rdfs:label>John</rdfs:label>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/person2">
<rdfs:label>Paul</rdfs:label>
</rdf:Description>
</rdf:Description>
This is syntactically invalid because an RDF processor expects the elements nested inside a description to be the properties of the resource - but here we just have other descriptions, which no indication as to what the actual relation between those description and the 'outer' description is. This can be fixed by further nesting each 'inner' description in a property, like so:
<rdf:Description>
<ex:contains>
<rdf:Description rdf:about="http://example.org/person1">
<rdfs:label>John</rdfs:label>
</rdf:Description>
<ex:contains>
<ex:contains>
<rdf:Description rdf:about="http://example.org/person2">
<rdfs:label>Paul</rdfs:label>
</rdf:Description>
<ex:contains>
<rdf:Description>
This is syntactially legal (assuming you also add a namespace definition to the rdf:RDF element for the ex namespace), and preserves the meaning of your original, separate descriptions. If you want to limit the number of times you have to repeat the ex:contains element, you can also use an RDF Collection, like so:
<rdf:Description>
<ex:contains rdf:parseType="Collection">
<rdf:Description rdf:about="http://example.org/person1">
<rdfs:label>John</rdfs:label>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/person2">
<rdfs:label>Paul</rdfs:label>
</rdf:Description>
<ex:contains>
<rdf:Description>
As said though: while this fixes the specific problem you asked about, neither of these solution are likely to help you much further. If the tool you're using to process this data can't deal with more than one rdf:Description, it's unlikely to be able to process either of the above files meaningfully. And also, as soon as you use any sort of proper RDF tooling to process/edit the file, it is likely to change this surface syntax again to something with more than one description. The point is that relying on the precise syntax formatting of the RDF/XML syntax is a Very Bad Idea(tm).
Related
One of my classes has a public property named Ttl. This is supposed to follow the CA1709 rules:
By convention, two-letter acronyms use all uppercase letters, and
acronyms of three or more characters use Pascal casing. The following
examples use this naming convention: 'DB', 'CR', 'Cpa', and 'Ecma'.
The following examples violate the convention: 'Io', 'XML', and 'DoD',
and for nonparameter names, 'xp' and 'cpl'.
Now, code analysis complains about my property, telling me it violates CA1704 (bad spelling).
I tried adding it to my CustomDictionary.xml like this:
<?xml version="1.0" encoding="utf-8" ?>
<Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CodeAnalysisDictionary.xsd">
<!-- Some unimportant elements are here in the real file -->
<Acronyms>
<CasingExceptions>
<Acronym>Ttl</Acronym> <!--Time To Live-->
</CasingExceptions>
</Acronyms>
</Dictionary>
I tried putting lower, upper and camel case into the dictionary, but none of them will remove the spelling complaint. Is there a way to add this acronym to the XML or do I just have to suppress the message for my properly named property?
You added "Ttl" as a casing exception. In fact it's not. It's three letters in Pascal case.
What you did not do is add "Ttl" as a word.
<Words>
<Recognized>
<Word>Ttl</Word>
Make sure you need it at all. Most .NET languages have "no abbreviations" as a good naming convention.
I'm using FOP 2.1 and am trying to set ViewerPreferences, e.g. DisplayDocTitle -> true.
I'm trying (from this question
<fo:declarations>
<pdf:dictionary type="Catalog" xmlns:pdf="http://xmlgraphics.apache/org/fop/extensions/pdf">
<pdf:dictionary type="normal" key="ViewerPreferences">
<pdf:entry key="DisplayDocTitle" type="boolean">true</pdf:entry>
</pdf:dictionary>
</pdf:dictionary>
<x:xmpmeta xmlns:x="adobe:ns:meta/">
...
but getting
Jul 13, 2016 11:18:31 AM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Unknown formatting object "{http://xmlgraphics.apache/org/fop/extensions/pdf}dictionary" encountered (a child of fo:declarations}. (See position 242:105)
Jul 13, 2016 11:18:31 AM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Unknown formatting object "{http://xmlgraphics.apache/org/fop/extensions/pdf}dictionary" encountered (a child of dictionary}. (See position 243:69)
and no ViewerPreferences inside the pdf.
When I put the dictionarys below the <x:xmpmeta xmlns:x="adobe:ns:meta/"> then I get no ViewerPreferences either, only pdfbox preflight will then complain about
The file test.pdf is not valid, error(s) :
7.3 : Error on MetaData, Cannot find a definition for the namespace http://xmlgraphics.apache/org/fop/extensions/pdf
What am I doing wrong, am I too early to try it? Where do I have to patch fop?
According to the release notes FOP 2.0 introduced, among other things, a
Low level mechanism to augment PDF /Catalog and /Page dictionaries
but there are not many examples of its usage in the website.
Looking at the testcases included in the source distribution, in particular the ones named pdf-dictionary-extension_*.xml, I was able to put together something similar to your code which does not generate run-time exceptions; admittedly, I'm not familiar enough with this PDF feature to say whether the output actually achieves what you are trying to do:
<fo:declarations>
<pdf:catalog xmlns:pdf="http://xmlgraphics.apache.org/fop/extensions/pdf">
<pdf:dictionary type="normal" key="ViewerPreferences">
<pdf:boolean key="DisplayDocTitle">true</pdf:boolean>
</pdf:dictionary>
</pdf:catalog>
</fo:declarations>
there is no <pdf:dictionary type="Catalog">, there is pdf:catalog instead
there is not a single <pdf:entry key="..." type="..."> element, but there is a specific element for each possible entry type: pdf:array, pdf:boolean, pdf:name, pdf:number, pdf:string, ...
(disclosure: I'm a FOP developer, though not very active nowadays)
As supplement to #lfurini's excellent finding, here are some more thing that can be done that way easily, tested with fop 2.1, but could also work from 2.0:, remove the comments from the relevant sections to try:
<fo:declarations>
<pdf:catalog xmlns:pdf="http://xmlgraphics.apache.org/fop/extensions/pdf">
<!-- this opens in full-screen mode, e.g. as presentation -->
<!-- pdf:name key="PageMode">FullScreen</pdf:name -->
<!-- this opens then second page so it is fully visible -->
<!-- (count seems to start at 0) -->
<!-- pdf:array key="OpenAction">
<pdf:number>1</pdf:number>
<pdf:name>Fit</pdf:name>
</pdf:array -->
<!-- this will replace the window title from filename to below dc:title -->
<pdf:dictionary type="normal" key="ViewerPreferences">
<pdf:boolean key="DisplayDocTitle">true</pdf:boolean>
</pdf:dictionary>
</pdf:catalog>
<x:xmpmeta xmlns:x="adobe:ns:meta/">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
<!-- Dublin Core properties go here -->
<dc:title>Sample Document title</dc:title>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
</fo:declarations>
Details of possible values can be looked up in the pdf specification (from page 139 in this v1.7 version, TABLE 3.25 Entries in the catalog dictionary), take care not to use values that would normally be set by fop anyway, restrict yourself to viewer/reader relevant stuff.
I have a class in my sourcecode:
public class TypeUserdef : SymbolType
which is a DTO for XML Serialization, now the code analysis reports a warning:
MSBUILD : warning CA1704: Microsoft.Naming : Correct the spelling of 'Userdef'
in type name 'TypeUserdef'.
I put the entry into the user dictionary (a customer dictionary in my project set to buildAction = "CodeAnalysisDictionary"):
<Dictionary>
<Words>
<Recognized>
<word>userdef</word>
</Recognized>
</Words>
</Dictionary>
Now the funny thing is the dictionary works very well for a lot terms. Just the term "userdef" and "vars" report errors. (Even spelling errors in the same code file can be ignored). What is so special about those terms above?
I found the Problem, while i know that xml is case sensitive it is quite difficult to spot that some entries in a few hundred lines of xml are lower cased...
<Word></Word> works as expected.
This is my first post here. I have just started working with Ruby and am using REXML for some XML handling. I present a small sample of my xml file here:
<record>
<header>
<identifier>oai:lcoa1.loc.gov:loc.gmd/g3195.ct000379</identifier>
<datestamp>2004-08-13T15:32:50Z</datestamp>
<setSpec>gmd</setSpec>
</header>
<metadata>
<titleInfo>
<title>Meet-konstige vertoning van de grote en merk-waardige zons-verduistering</title>
</titleInfo>
</metadata>
</record>
My objective is to match the last numerical value in the tag with a list of values that I have from an array. I have achieved this with the following code snippet:
ids = XPath.match(xmldoc, "//identifier[text()='oai:lcoa1.loc.gov:loc.gmd/"+mapid+"']")
Having got a particular identifier that I wish to investigate, now I want to go back to and select and then select to get the value in the node for that particular identifier.
I have looked at the XPath tutorials and expressions and many of the related questions on this website as well and learnt about axes and the different concepts such as ancestor/following sibling etc. However, I am really confused and cannot figure this out easily.
I was wondering if I could get any help or if someone could point me towards an online resource "easy" to read.
Thank you.
UPDATE:
I have been trying various combinations of code such as:
idss = XPath.match(xmldoc, "//identifier[text()='oai:lcoa1.loc.gov:loc.gmd/"+mapid+"']/parent::header/following-sibling::metadata/child::mods/child::titleInfo/child::title")
The code compiles but does not output anything. I am wondering what I am doing so wrong.
Here's a way to accomplish it using XPath, then going up to the record, then XPath to get the title:
require 'rexml/document'
include REXML
xml=<<END
<record>
<header>
<identifier>oai:lcoa1.loc.gov:loc.gmd/g3195.ct000379</identifier>
<datestamp>2004-08-13T15:32:50Z</datestamp>
<setSpec>gmd</setSpec>
</header>
<metadata>
<titleInfo>
<title>Meet-konstige</title>
</titleInfo>
</metadata>
</record>
END
doc=Document.new(xml)
mapid = "ct000379"
text = "oai:lcoa1.loc.gov:loc.gmd/g3195.#{mapid}"
identifier_nodes = XPath.match(doc, "//identifier[text()='#{text}']")
record_node = identifier_nodes.first.parent.parent
record_node.elements['metadata/titleInfo/title'].text
=> "Meet-konstig"
I need to select some nodes from an XML file (AppNamespace.xaml from a Silverlight XAP file, not that it matters), but the file has namespace stuff so XPath doesn't work. I could waste most of a day trial-and-erroring the bondage-and-discipline nightmare of XmlNamespaceManager and end up with hopelessly fragile code that can't tolerate the slightest variation in the input file (not a great idea in production code), or I could use the ludicrous local-name() syntax[1].
But it would be more convenient to use XPath as a human-readable query language that can be used to return specified nodes or attribute values from arbitrary XML files.
So is there any way to strip the line-noise out of the file? Or am I stuck? Is the labyrinthine imbecility of Linq-to-XML truly the lesser evil?
[1]
//*[local-name() = 'Deployment']/*[local-name() = 'Deployment.Parts']/*[local-name() = 'AssemblyPart']/#*[local-name()='Name']
Update
Five years down the road, I stand behind the term "labyrinthine imbecility" with every fiber of my being, except for a few fibers that want to use something much stronger.
Ed, here's an example of using namespaces with the System.Xml.XPath Extensions class. I've modified it to match the input you're looking at:
string markup = #"
<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ...>
<Deployment.Parts>
<AssemblyPart x:Name="xamlName" Source="assembly" />
</Deployment.Parts>
</Deployment>
";
XmlReader reader = XmlReader.Create(new StringReader(markup));
XElement root = XElement.Load(reader);
XmlNameTable nameTable = reader.NameTable;
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);
nsm.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
nsm.AddNamespace("dep", "http://schemas.microsoft.com/client/2007/deployment");
IEnumerable<XElement> elements =
root.XPathSelectElements("//dep:Deployment/dep:Deployment.Parts/dep:AssemblyPart/#x:Name", nsm);
foreach (XElement el in elements)
Console.WriteLine(el);
Not very complicated. Obviously you already know about XmlNamespaceManager, but I think you got a worse impression of it than it deserves.
When you say "hopelessly fragile code that can't tolerate the slightest variation in the input file", are you blaming namespaces in general, or XmlNamespaceManager? I don't see how either one makes it fragile... any more so than XML processing code without namespaces will not tolerate certain changes in the input document, but will tolerate others.
Have a little respect for other intelligent people in the industry, take a little time to understand the advantages behind a design before you dismiss it, and you will usually find that there are good reasons for what was done.
Not that XML namespaces couldn't be improved upon. However nobody has managed to produce a better standard and get it accepted by the community.
In XPath 2.0 you can use namespace wildcards (if you know what you are doing):
//*:Deployment/*:Deployment.Parts/*:AssemblyPart/#Name
btw. If an attribute doesn't have a prefix it is in no namespace at all. As this is most often the case, I guess, you don't need local-name() for the attribute.
I came here as a result of this search:
and I am adding an "Answer" to cheer on your "5 years on" update.
I was motivated to do this because I have an XML document that uses a tonne of namespaces -
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:x2="urn:schemas-microsoft-com:office:excel2" version="1.0" exclude-result-prefixes="msxsl">
and APPARENTLY I have to know what all those namespaces are in advance in order to hard code the XmlNamespaceManager, or write some code that parses the namespace declarations and adds the relevant name spaces myself. Why in the name of all that is holy does the XmlDocument not manage to do that all by itself?
XmlDocument databaseXml = new XmlDocument();
databaseXml.LoadXml(xslt.XslTransform);
var dbnsmgr = new XmlNamespaceManager(databaseXml.NameTable);
dbnsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
dbnsmgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
XmlElement databaseStylesElement = (XmlElement)database
Xml.DocumentElement.SelectSingleNode("/xsl:stylesheet/xsl:template");