How to properly add acronyms to CustomDictionary.xml - visual-studio

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.

Related

Wildcard XPath nested element selection by example

I am being given XML that looks like this:
<?xml version='1.0' encoding='UTF-8'?>
<singleton-list>
<fizzbuzz>
<amountId>123</amountId>
<countryId>456</countryId>
<action>Overwrite</action>
</fizzbuzz>
</singleton-list>
However, the outer-most element (in this case, singleton-list) will be different each time, and will be one of many different values. For instance I might get another XML message that looks like this:
<?xml version='1.0' encoding='UTF-8'?>
<aggregateList>
<fizzbuzz>
<amountId>123</amountId>
<countryId>456</countryId>
<action>Overwrite</action>
</fizzbuzz>
</aggregateList>
I'm trying to write an XPath that selects the fizzbuzz out of each message I receive, regardless of what the outer-most element is (singleton-list, aggregateList, or otherwise).
I think I could do something involving a wildcard, but I'm not sure if asterisks have special meaning in XPath or if I'm going about this the wrong way.
My best attempt at an XPath to do this selection is:
/*/fizzbuzz
Is this correct or is there a better way to do this?
either you can use //fizzbuzz or /*/fizzbuzz.

How to ignore XML declaration differences with XmlUnit?

How can I configure XmlUnit.Net to ignore the XML declaration when comparing two documents?
Assume I have the following control document:
<?xml version="1.0" encoding="utf-8"?>
<a><amount>1</amount></a>
Which I want to compare with:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a><amount>1</amount></a>
The comparison should result in no differences.
My expectation would be that using a NodeFilter like so should work, but it doesn't:
var diff = DiffBuilder.Compare(control)
.WithTest(test)
.WithNodeFilter(n => n.NodeType != XmlNodeType.XmlDeclaration)
.Build();
diff.Differences.Count().Should().Be(0);
The assertion fails with two differences - one for the encoding (different casing) and another for the standalone attribute. I'm not interested in any.
Whether I say n.NodeType != XmlNodeType.XmlDeclaration or n.NodeType == XmlNodeType.XmlDeclaration makes no difference.
I am using XMLUnit.Core v2.5.1.
NodeFilter only applies to nodes that are children of other nodes (returned by XmlNode.ChildNodes). Unfortunately this is not the case for the document type declaration, which probably is a bug.
In your case you want to tweak the DifferenceEvaluator and downgrade the differences you are not interested in. Something like
DifferenceEvaluators.Chain(DifferenceEvaluators.Default,
DifferenceEvaluators.DowngradeDifferencesToEqual(ComparisonType.XML_STANDALONE, ComparisonType.XML_ENCODING))
would swallow the differences.
Maybe you don't want to just count the differences but also look at their severity. The difference in encoding would be a "similar" difference, while the different values of standalone are critical.

Nokogiri not parsing XML in ruby - xmlns issue?

Given the following ruby code :
require 'nokogiri'
xml = "<?xml version='1.0' encoding='UTF-8'?>
<ProgramList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='http://publisher.webservices.affili.net/'>
<TotalRecords>145</TotalRecords>
<Programs>
<ProgramSummary>
<ProgramID>6540</ProgramID>
<Title>Matalan</Title>
<Limitations>A bit of text
</Limitations>
<URL>http://www.matalan.co.uk</URL>
<ScreenshotURL>http://www.matalan.co.uk/</ScreenshotURL>
<LaunchDate>2009-11-02T00:00:00</LaunchDate>
<Status>1</Status>
</ProgramSummary>
<ProgramSummary>
<ProgramID>11787</ProgramID>
<Title>Club 18-30</Title>
<Limitations/>
<URL>http://www.club18-30.com/</URL>
<ScreenshotURL>http://www.club18-30.com</ScreenshotURL>
<LaunchDate>2013-05-16T00:00:00</LaunchDate>
<Status>1</Status>
</ProgramSummary>
</Programs>
</ProgramList>"
doc = Nokogiri::XML(xml)
p doc.xpath("//Programs")
gives :
=> []
Not what is expected.
On further investigation if I remove xmlns='http://publisher.webservices.affili.net/' from the initial <ProgramList> tag I get the expected output.
Indeed if I change xmlns='http://publisher.webservices.affili.net/' to xmlns:anything='http://publisher.webservices.affili.net/' I get the expected output.
So my question is what is going on here? Is this malformed XML? And what is the best strategy for dealing with it?
While it's hardcoded in this example the XML is (will be) coming from a web service.
Update
I realise I can use the remove_namespaces! method but the Nokogiri docs do say that it's "...probably is not a good thing in general" to do this. Also I'm interested in why it's happening and what the 'correct' XML should be.
The xmlns='http://publisher.webservices.affili.net/' indicates the default namespace for all elements under the one where it appears (including the element itself). That means that all elements that don’t otherwise have an explicit namespace fall under this namespace.
XPath queries don’t have default namespaces (at least in XPath 1.0), so any name that appears in one without a prefix refers to that element in no namespace.
In your code, you want to find Program elements in the http://publisher.webservices.affili.net/ namespace (since that is the default namespace), but are looking (in your XPath query) for Program elements in no namespace.
To explicitly specify the namespace in the query, you can do something like this:
doc.xpath("//pub:Programs", "pub" => "http://publisher.webservices.affili.net/")
Nokogiri makes this a little easier for namespaces declared on the root element (as in this case), declaring them for you with the same prefix. It will also declare the default namespace using the xmlns prefix, so you can also do:
doc.xpath("//xmlns:Programs")
which will give you the same result.

Custom Dictionary not working with Code Analysis

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.

XPath query for node names matching a certain pattern

Is there a way to apply XPath's starts-with function to a node's name instead of its value? I want to select the FOObar and FOObaz nodes from the following XML document without selecting the notFOO node:
<?xml version="1.0" encoding="UTF-8" ?>
<RootNode>
<FOObar xmlns="http://sample.example.com">
<value>numOne</value>
</FOObar>
<FOObaz xmlns="http://sample.example.com">
<value>numTwo</value>
</FOObaz>
<notFOO xmlns="http://sample.example.com">
<value>numThree</value>
</notFOO>
</RootNode>
I get that it's possible to use starts-with to search based on text nodes, e.g.
//sample:value[starts-with(.,'num')]
Is there a way to write the following that is syntactically valid?
//sample:[starts-with(node(),'FOO')]
This question originally came with an SSCCE, but now that the question is answered, all that code is just clutter. It's still available in the revision history, of course.
Use the name() or local-name() functions to refer to nodes by name:
//*[starts-with(local-name(), 'FOO')]

Resources