XML Schema validation using the XSD - xsd

I have an XML which looks like below in which the numbers of Items can vary from 0 to n. Is there a way to write XSD for verifying the Schema .
<?xml version="1.0" encoding="utf-8" ?>
<ShoppingItems>
<CustomerName>John</CustomerName>
<Address>Walstreet,Newyork</Address>
<Item1>Milk</Item1>
<Price1>1$</Price1>
<Item2>IceCream</Item2>
<Price2>1$</Price2>
<Item3>Bread</Item3>
<Price3>1$</Price3>
<Item4>Egg</Item4>
<Price4>1$</Price4>
<Item..n>Egg</Item..n>
<Price..n>1$</Price..n>
</ShoppingItems>

Not in it's current form. An XSD definition is very strict - in the above case you would have specify every possible ShoppingItems type (those including Item..n and Price..n) which is of course not possible.
What would be better is to change the XML file so that it is more logically structured:
<?xml version="1.0" encoding="utf-8" ?>
<ShoppingItems>
<CustomerName>John</CustomerName>
<Address>Walstreet,Newyork</Address>
<Items>
<Item price="1$">Milk</Item>
<Item price="3$">IceCream</Item>
<Item price="1$">Bread</Item>
<Item price="1.5$">Egg</Item>
</Items>
</ShoppingItems>
It is now completely possible to define this document with a schema.

Related

ignore node using XSLT

I have below xml file which is coming form my vendor.
<?xml version="1.0" encoding="UTF-8"?>
<nm:MT_employee xmlns:nm="http://firstscenario.com"xmlns:tl="http://secondscenario.com">
<EMployeeDetails>
<Name>Janardhan</Name>
<id>1234</id>
<Address>India</Address>
</EMployeeDetails>
<tl:Extension>
<tl:Number>5678</tl:Number>
<tl:Salary>2345678</tl:Salary>
</tl:Extension>
</nm:MT_employee>
In the above xml I want to ignore the entire tl:Extension node. the final output should be like below
<?xml version="1.0" encoding="UTF-8"?>
<nm:MT_employee xmlns:nm="http://firstscenario.com"xmlns:tl="http://secondscenario.com">
<EMployeeDetails>
<Name>Janardhan</Name>
<id>1234</id>
<Address>India</Address>
</EMployeeDetails>
</nm:MT_employee>
I tried to with different XSLT codes but it's not working. Could you please suggest how can I achieve this?
Regards,
Janardhan
The general rule to "ignore" an element from the source XML
is to write an "empty" template for this element, in your case:
<xsl:template match="tl:Extension"/>
As this template refers to tl namespace, it must be specified
in the xsl:transform tag.
Of course, to copy the rest of the source content, your script
must include the identity template.
Below you have an example script:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tl="http://secondscenario.com" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="tl:Extension"/>
<xsl:template match="#*|node()">
<xsl:copy><xsl:apply-templates select="#*|node()"/></xsl:copy>
</xsl:template>
</xsl:transform>

Does any XSD edge case allow XML element content inside a text node?

Does any XSD edge case allow (unescaped) XML element content inside a text node? E.g. can you put a CDATA element inside a tag defined as xs:string and have it validate (without declaring mixed content)?
If you have an element that contains a string i.e.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2018 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root" type="xs:string" />
</xs:schema>
Then that can contain CDATA i.e.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Temp\XSDFile2.xsd">
Optional Text
<![CDATA[
<someXmlData></someXmlData>
]]>
Optional Text
</Root>
As this passes though some parsers it may get escaped back to this, but both are valid and equivalent.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XSDFile2.xsd">
Optional Text
<someXmlData></someXmlData>
Optional Text
</Root>

ImageView or ImageButton highlight?

How make pressed effect like on attached sreenshots??
Normal state
Pressed
The easiest way to achieve this is to use a selector for drawable. A selector defines a set of multiple images for different states of a view.
Create an xml with following contents in your drawable folder and set it as the background or src of your view(Button/ImageButton).
eg:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blue_button_pressed" android:state_selected="true"/>
<item android:drawable="#drawable/blue_button_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/blue_button_default"/>
</selector>
Use two different images for your pressed and normal states.

Manifest file for registration of event handler in SP 2007

I'm trying to create a feature to register my event handler to a specific list. I'm building it like I would a normal feature. However, I'm not sure how to define my Elements file. The file for my feature is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="101">
<Receiver>
<Name>AddedEventHandler</Name>
<Type>ItemAdded</Type>
<SequenceNumber>10000</SequenceNumber>
<Assembly>ChangeContentTypeEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7bfc7c17f98031d7</Assembly>
<Class>ChangeContentTypeEventHandler.ChangeContentTypeEventHandler</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>
Based on this, how would I set up the file for registration of this event handler?
Also, here is my Feature.xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Feature Scope="Web" Title="Change Content Type Event Handler"
Id="{27C2FDFF-ADA0-4984-955C-6448E182FA88}"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="ListTemplates\ListManifest.xml"/>
<ElementFile Location="Messages\schema.xml"/>
</ElementManifests>
</Feature>
Also, can this be part my solution package WSP for the feature itself so that it gets deployed with the feature?
Thanks in advance
You have two options here. Either you create a custom list template so that you can specify the id of your custom list template in your elements file, or you have to attach your event receiver through code (feature receiver).
The problem with registering an event receiver declaratively is that you can only specify the list template id (effecting all lists based on the specified template), but not a single list name or id.
You can find an example of such a feature receiver in this article: http://www.sharepointdev.net/sharepoint--development-programming/whether-there-is-a-way-to-register-a-custom-event-handler-at-a-special-list-63446.shtml

XSD and element existence requirement, or other options

At the XSD level, can I define an element requirement based on presence of another element in the document?
For example, I want to require the element <firstname /> be provided whenever the <id /> element is also present, otherwise first name is optional.
If this cannot be enforced at the XSD level, then should I make both elements optional and enforce the requirement through a different level of checking?
Options welcome.
This cannot be enforced at the XSD level.
Making both elements optional and enforcing the requirement at a different level in the application architecture sounds like a good option to me.
Here's an example of this being done in a DTD. (Note: The test instances were validated using oXygen XML Editor (which is using Xerces).)
DTD (test.dtd)
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT user ((firstname?|(firstname,id)),lastname)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT id (#PCDATA)>
Here are some example XML instances (valid and invalid):
Valid
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE user SYSTEM "test.dtd">
<user>
<firstname/>
<id/>
<lastname/>
</user>
Invalid
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE user SYSTEM "test.dtd">
<user>
<id/>
<lastname/>
</user>
Valid
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE user SYSTEM "test.dtd">
<user>
<firstname/>
<lastname/>
</user>

Resources