Category Archives: Misc - Page 12

Validate XML with multiple namespaces using xsd

This took some time to figure out. They do not make it easy for us 🙂

I am here going to show the basics of validating an XML with multiple namespaces. In the example I will use two namespaces. XML to validate looks like this:

<?xml version="1.0"?>
<ns:books   xmlns="http://www.my.com"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

			xsi:schemaLocation="http://www.my.com/book  book.xsd
                    			http://www.my.com/books books.xsd"
			xmlns:cs="http://www.my.com/book"
			xmlns:ns="http://www.my.com/books">
    
    <cs:book author="Astrid Lindgren"/>
</ns:books>

Here ‘books’ are defined in the ‘ns’ (“http://www.my.com/books”) namespace and ‘book’ is defined in the ‘cs’ (“http://www.my.com/book”) namespace.
We start with defining the ‘ns’ (xmlns:ns=”http://www.my.com/books”) and ‘cs’ (“xmlns:cs=”http://www.my.com/book”) prefix.
We then point out the different xsd files, one for each namespace, with the “schemaLocation” tag. This tag takes the namespace uri and file path as arguments (one on each line is a good rule to keep things tidy)

We then start defining the xsd for the ‘books’ tag (books.xsd):

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.my.com/books" 
            xmlns:cs="http://www.my.com/book"
            elementFormDefault="qualified">
                
<xs:import namespace="http://www.my.com/book" 
       schemaLocation="book.xsd"/>
       
    <xs:element name="books">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="cs:book"/>      
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Here we set the “targetNamespace” to our books uri. We also have to define ‘xmlns:cs=”http://www.my.com/book”‘ due to the book tag inside our books element (cs:book). The schema also needs to know where the book definition is so we import the book xsd with the import-tag
After the import we use the ref attribute to reference the book element. This element is defined in the book.xsd file:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.my.com/book" 
            elementFormDefault="qualified">

	<xs:element name="book">
    	<xs:complexType>
	      	<xs:attribute name="author" type="xs:string"/>
    	</xs:complexType> 
	</xs:element>      
</xs:schema>

The only thing to think about here is to set the “targetNamespace” to the book uri (“http://www.my.com/book”)

My SVN cheat sheet

Here is my “Cheat sheet” for the Subversion version system

Create repository

svnadmin create <project1>

This will create a repository at your current location named “project1”

Import a project locally

svn import /path/to/files/to/import file:///path/to/repository -m "Initial import"

Import a project into remote server (running svnserve on server)

svn import /path/to/files/to/import svn:///path/to/repository -m "Initial import"

Checkout locally

svn co file://<path to repository> <foldername>

<foldername> will be created if it does not exists. NOTE If you are using branches/tags/trunk structure don’t forget to include it in the path (eq. /path/to/repository/trunk).

Checkout remote (with svnserve running on server)

svn co svn://<host/path/to/repository>/<foldername>

Checkout remote (svnserve and authentication)

svn co --username=<user>  svn://<host/path/to/repository>/<folder>

List svn directory (to look at directory contents)

svn list file://<path to repository>/<foldername>

Use ‘svn://…’ for remote ‘look’ in the same way as with checkout. This will display thew latest revision. To see another revision use @revision (at the end of the path)

Commit changes (all files)

svn commit -m"<commit message>"

Commit one file

svn commit -m"<commit message>" filename

To commit more then one file just place their path after the first ‘filename’

Check status of your working copy

svn status

NOTE You have to stand inside the working directory for the command to work

Get info about a repository (eg. URL, root)

svn info

Conflict resolving
Resolvetypes:

  • mine-full: use your version only
  • theirs-full: use the latest version in the repository
  • base: use the version you used before making any changes
  • working: this is the option if you want to do a manual merge of the file. Removing the changes you don’t want and keeping the ones that you want on a row by row basis

Resolve command:

svn resolve --accept resolvetype filename

Ignore files and folders globally
In the section [miscellany] in your subversion conf file (OS X: /User/<username>/.subversion/conf) there is a setting called global-ignores
Example:

global-ignores = .* old *.gz tmp

This ignores hidden files/folders (all which name starts with a dot (.)), all files/folders called old or tmp and all files ending with .gz

Ignore files and folders locally
Run this command from the folder above the files/folders that should be ignored:

svn propedit svn:ignore . #NOTE the last dot!

This opens up your chosen editor (ENV variable EDITOR=editor) so you can add your ignore files/folders. One ignore per line.
Example:

.*
old
*.gz
tmp

When you save and exit the properties are written to that folder

Add folder non recursive
By default svn add is recursive so all files and folders in the folder you add will be added automatically. To just add the one folder and not the content of that folder use the –non-recursive option

svn add --non-recursive foldername

This will only add the folder foldername and not any files or folders inside foldername

Validate elements that can be empty with XSD

Say you have an element looking like this:


    Catcher in the rye

and that element is allowed to also be empty. Like this:


    <book/>

How do you validate it? One way is to use the attribute “nillable” like this


  
    
      <xs:element name="book" type="xs:string" nillable="true"/>
        
      
 

This will validate both a “book” element that is empty and one that is not