Can XPathSelectElement ignore case? - c#

Is there a way to ignore case when we try to use XPathSelectElement or any operation like to retrieving attributes from XDocument? The purpose for asking this question is that, I have some configuration files (xml) and I am writing a generic code that will read the config files to get required information for XPathSelectElement. Also, I try to get the values of attributes. Even if someone puts the nodes/attributes in different case, my program should be able to work without fail.
I use C#/.Net 3.5.

You can't ignore case with XPath. You can accomodate, though.
For example - elements, assuming they contain letters in the ASCII range only:
//*[
translate(
name(),
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz'
) = 'myname'
]
Attributes would work the same (with #* in place of *).
If you do not want to bloat your XPath expressions with this, you could lower-case all element- and attribute names beforehand, for example via XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node() | #*" />
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{translate(name(), $upper, $lower)}">
<xsl:apply-templates select="node() | #*" />
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{translate(name(), $upper, $lower)}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

Before you load the XML string make lower-case. That will solve the issue. I use this method myself.

Related

Remove Certain HTML tags in C#

I'm trying to remove a certain html tags in C# like this:
<div>
<blockquote style="font-size: 30px" width="300px">
For 50 years, WWF has been protecting the future of nature. The world's leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United States and close to 5 million globally.
</blockquote>
</div>
To be result as
<div>For 50 years, WWF has been protecting the future of nature. The world's leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United States and close to 5 million globally.</div>
So far, I'm trying to do the regex. (<.+?)\s+style\s*=\s*([""']).*?\2(.*?>) but this is only for removing the style but I'm not sure how can I able to achieve the result that I want.
Thanks!
As far as I can see, you want to remove the HTML elements that contain a style attribute, also remove their closing pairs. Unfortunately, there is no good way to do that with regexes. Without the 'also remove their closing pairs' clause, we could write an approximately good regex.
On the other hand, XSLT is the right tool for this, because it can handle the recursive nature of XML:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//*[not(#style)]">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
What's happening here? The <xsl:template match="//*[not(#style)]"> part matches everything that does not have a style attribute. Then the <xsl:copy>...</xsl:copy> part copies them entirely. I.e. the items that have a style attribute, they will not be copied.
For the record, this is a slight variant of the XSLT identity transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

c# How to get attribute name from xslt

I have following part of xslt in string format :-
<xsl:if test="TestValue3 and TestValue3 != ''">
<xsl:attribute name = "TestDate" >
<xsl:value-of select = "TestValue3" />
</xsl:attribute>
</xsl:if>
I just want to fetch its attribute name from c# code.
Attribuute Name= TestDate
How can I achieve this?
Use your favourite XML API to load the XSLT and iterate/query the item in question (in this case you would need to look for the owning xsl:if and the condition itself). e.g. you could load it into an XmlDocument or XDocument.
You can use XPath to find the element for XmlDocuments or if you use XDocument you can use LINQ.
Do not attempt to use technologies that are not equipped for structured data.
i.e.
don't use flat string search
don't use regex
Actually I am adding above block into existing xslt ,, but before adding I need to check if attribute name =TestDate already exists ... For that I need to know attribute name , becase attribute name can vary as per block,, it is not fix each time
Again, use the above recommendations. Both XmlDocument and XDocument allow for load/edit/save.
If I understand you correctly this should do the job, otherwise please add more context.
<xsl:if test="TestValue3 and TestValue3 != ''">
<xsl:if test="not(#TestDate)">
<xsl:attribute name = "TestDate" >
<xsl:value-of select = "TestValue3" />
</xsl:attribute>
</xsl:if>
</xsl:if>

How to select an attribute value in an XML and concatenate it with a string and use it as an attribute value in a new XML using XSLT

I need to transform an existing XML into another XML using XSLT.
The problem I am facing is that I need to use the "typeName" attribute from the ECClass and concatenate it with http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1#
The XML i am working with is -
<ECSchema>
<ECClass typeName="ABC">
<BaseClass>PQR</BaseClass>
<BaseClass>XYZ</BaseClass>
</ECClass>
<ECClass typeName="IJK">
<BaseClass>MNO</BaseClass>
<BaseClass>DEF</BaseClass>
</ECClass>
<ECSchema>
For example the concatenated result should be -
http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1#ABC for the first ECClass
I need to set this string as the attribute value of rdf:about in the owl:class tag in the new XML structure.
The new XML structure is -
<owl:ontology rdf:about="http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1">
<owl:class rdf:about="http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1#ABC">
</owl:class>
<owl:class rdf:about="http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1#ABC">
</owl:class>
</owl:ontology>
Right now I have not yet tried to do anything about the BaseClass. I have only been trying to convert the ECCLass to owl:class.
The XSL for it is -
<xsl:template match="/">
<owl:Ontology rdf:about="http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1"/>
<xsl:for-each select="ECSchema/ECClass">
<owl:class rdf:about="<xsl:value-of select="concat('http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1#' , '#typeName') />" >
</owl:class>
</xsl:for-each>
</xsl:template>
I have been trying many combinations to do this from various sources but haven't been able to do it.
It always returns an error - "Additional information: '<', hexadecimal value 0x3C, is an invalid attribute character."
Can anybody please help me with this as I am very new to XSLT and all I have been getting is lots of errors.
Tags cannot be nested. To achieve your purpose, you should learn about attribute value templates. In addition, your code is rather sloppy. Try it this way:
<xsl:template match="/">
<owl:Ontology rdf:about="http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1">
<xsl:for-each select="ECSchema/ECClass">
<owl:class rdf:about="{concat('http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1#', #typeName)}" />
</xsl:for-each>
</owl:Ontology>
</xsl:template>
or perhaps a bit more elegant:
<xsl:variable name="myURL">http://www.semanticweb.org/aman.prasad/ontologies/2015/5/untitled-ontology-1</xsl:variable>
<xsl:template match="/">
<owl:Ontology rdf:about="{$myURL}">
<xsl:for-each select="ECSchema/ECClass">
<owl:class rdf:about="{$myURL}#{#typeName}" />
</xsl:for-each>
</owl:Ontology>
</xsl:template>

Bind value to xslt variable with choose-when-otherwise

I am using xslt to transform xml to xml with c#. Below is an extract from the xslt where variables assignment is being shown.
<xsl:variable name="testvar">
<xsl:choose>
<xsl:when test="$condition">
<xsl:value-of select="myUtils:Method1($var1,$var2)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="myUtils:Method2($var1,$var2)" /> <!--Method1 and Method 2 are written in c# code.-->
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
The above assignment is different from the below assignment:
<xsl:variable name="testvar" select="myUtils:Method1($var1,$var2)"/>
Another variable depends on the above variable as below:
<xsl:variable name="testvar2" select="$testvar/node()[1]/node()[1]/node()[1]/node()[1]"/>
This variable is assigned the expected value when $testvar is assigned in the second way.
The return type of both Method1 and Method2 is XmlDocument. I think there is something wrong with <xsl:value-of>. What is the correct way of assigning this variable?
UPDATE
I have solved the issue by the following code:
<xsl:variable name="testvar"><xsl:copy-of select="myUtils:Method1($var1,$var2)"/></variable>
For the second variable I have used the below code:
<xsl:variable name="testvar2" select="msxsl:node-set($testvar)/node()[1]/node()[1]/node()[1]/node()[1]"/>
Please refer to this link for more details.
I am guessing your Method1 functions returns node-set, as opposed to simple text/number value. In this this case the issue is probably because xsl:value-of gets the "value" of a node, not the actual node itself.
Try using xsl:copy-of instead
<xsl:copy-of select="myUtils:Method1($var1,$var2)" />

XPath problems in C# XSLT transformation

I am trying to parse an XML document to a website, through a XSLT transformation.
However, to make it work I have to use the following XPath:
/*[name()='standards']/*[name() = 'standard']
Why does the following XPath expression not work?
/standards/standard
Your problem is the most FAQ in XPath -- search for XPath and default namespace and you'll find many good answers.
To summarize the problem: XPath interpretes any unprefixed name as belonging to "no namespace".
Therefore any unprefixed name in any XPath expression, belonging to some default namespace (not the "no namespace") isn't selected.
One way to continue to use names in the location steps is to indicate to the XPath processor that a specific prefix, say "x" is associated to the default namespace. Then issue:
/x:standards/x:standard
In .NET such namespace binding (called "registering of namespace") is done using the XmlNamespaceManager class. See this complete example.
In XSLT, simply define a namespace at a global level, then specify XPath expressions where each element name is prefixed by the prefix so defined.
Here is a small example:
<nums xmlns="some:nums">
<num>01</num>
<num>02</num>
<num>03</num>
<num>04</num>
<num>05</num>
<num>06</num>
<num>07</num>
<num>08</num>
<num>09</num>
<num>10</num>
</nums>
To process the above XML document we have this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="some:nums">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:value-of select="/x:nums/x:num[. = 3]"/>
</xsl:template>
</xsl:stylesheet>
Applying this transformation to the above XML document correctly selects the wanted element and outputs its string value:
03
I don't know what your question is. Just taking a wild stab, perhaps this is what you want ...
<xsl:stylesheet version="1.0"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ikas="http://www.ikas.dk"
exclude-result-prefixes="msxsl ikas">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<div xmlns="http://www.ikas.dk">
<textarea>
<xsl:copy-of select="/ikas:standards/ikas:standard"/>
</textarea>
</div>
</xsl:template>
</xsl:stylesheet>

Categories

Resources