/// <summary>
        /// Gets the parent ESD metadata terms of the specified term
        /// </summary>
        /// <param name="termId">The id of the term for which to get parent terms</param>
        /// <param name="defaultOnly">ESD metadata lists are polyhierarchical - terms can appear in multiple positions, but before the SKOS alignment each had a single default position</param>
        /// <returns>A collection of broader terms. If <code>defaultOnly</code> is <code>true</code>, the collection will contain a maximum of one term.</returns>
        public EsdTermCollection GetBroaderTerms(string termId, bool defaultOnly)
        {
            // get mappings to broader terms
            string xPath;

            if (defaultOnly)
            {
                // account for GCL or all SKOS-compatible lists which don't have default attribute
                xPath = String.Format(CultureInfo.InvariantCulture, "ns:Item[@Id='{0}']/ns:BroaderItem[@Default='true' or count(@Default)=0]", termId.Trim());
            }
            else
            {
                xPath = String.Format(CultureInfo.InvariantCulture, "ns:Item[@Id='{0}']/ns:BroaderItem", termId.Trim());
            }
            XPathNodeIterator it = this.SelectNodes(xPath);

            // get details of broader terms
            EsdTermCollection terms = new EsdTermCollection();

            while (it.MoveNext())
            {
                EsdTerm term = this.GetTerm(it.Current.GetAttribute("Id", ""), EsdPreferredState.Preferred);
                if (term != null)
                {
                    terms.Add(term);
                }
            }

            terms.Sort();
            return(terms);
        }
        /// <summary>
        /// Gets the ESD metadata preferred terms related to the specified term
        /// </summary>
        /// <param name="termId">The id of the term for which to get related terms</param>
        /// <param name="termIdToExclude">The id of a term which should not be included even if it is related to the specified term</param>
        /// <returns>A collection of related terms</returns>
        public EsdTermCollection GetRelatedTerms(string termId, string termIdToExclude)
        {
            // get mappings to related terms
            string xPath;

            if (termIdToExclude != null && termIdToExclude.Length > 0)
            {
                xPath = String.Format(CultureInfo.InvariantCulture, "ns:Item[@Id='{0}']/ns:RelatedItem[@Id!='{1}']", termId.Trim(), termIdToExclude.Trim());
            }
            else
            {
                xPath = String.Format(CultureInfo.InvariantCulture, "ns:Item[@Id='{0}']/ns:RelatedItem", termId.Trim());
            }
            XPathNodeIterator it = this.SelectNodes(xPath);

            // get details of mapped terms
            EsdTermCollection terms = new EsdTermCollection();

            while (it.MoveNext())
            {
                EsdTerm term = this.GetTerm(it.Current.GetAttribute("Id", ""), EsdPreferredState.Preferred);
                if (term != null)
                {
                    terms.Add(term);
                }
            }

            terms.Sort();
            return(terms);
        }
        /// <summary>
        /// Gets ESD metadata terms based on the name of the term
        /// </summary>
        /// <param name="termName">The term name to search for</param>
        /// <param name="exactMatch">true to match the whole name of a term; false to match any part of the term</param>
        /// <param name="preferredState">Specify whether the term must be preferred or non-preferred</param>
        /// <returns>A collection of matching terms</returns>
        /// <remarks>Matching is not case-sensitive. For SKOS-compatible lists (those published after June 2010) the behaviour
        /// of this method has changed. When a search matches a non-preferred term it used to return the non-preferred term
        /// as a top-level object alongside preferred terms. For SKOS compatible lists it now returns the preferred term which
        /// goes with the non-preferred term.</remarks>
        public EsdTermCollection GetTerms(string termName, bool exactMatch, EsdPreferredState preferredState)
        {
            // double-quotes (") are used to delimit xpath strings in this method because it allows apostrophes in the string

            string xPath = null;

            if (exactMatch)
            {
                switch (preferredState)
                {
                case EsdPreferredState.Preferred:
                    xPath = this.skosCompatible ? "ns:Item[ns:Name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=\"{0}\"]]"
                            : "ns:Item[@Preferred='true' and ns:Name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=\"{0}\"]]";
                    break;

                case EsdPreferredState.NonPreferred:
                    xPath = this.skosCompatible ? "ns:Item[ns:AlternativeName[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=\"{0}\"]]"
                    : "ns:Item[@Preferred='false' and ns:Name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=\"{0}\"]]";
                    break;

                case EsdPreferredState.Any:
                    xPath = this.skosCompatible ? "ns:Item[ns:Name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=\"{0}\"] or ns:AlternativeName[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=\"{0}\"]]"
                            : "ns:Item[ns:Name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=\"{0}\"]]";
                    break;
                }
            }
            else
            {
                switch (preferredState)
                {
                case EsdPreferredState.Preferred:
                    xPath = this.skosCompatible ? "ns:Item[ns:Name[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), \"{0}\")]]"
                            : "ns:Item[@Preferred='true' and ns:Name[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), \"{0}\")]]";
                    break;

                case EsdPreferredState.NonPreferred:
                    xPath = this.skosCompatible ? "ns:Item[ns:AlternativeName[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), \"{0}\")]]"
                            : "ns:Item[@Preferred='false' and ns:Name[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), \"{0}\")]]";
                    break;

                case EsdPreferredState.Any:
                    xPath = this.skosCompatible ? "ns:Item[ns:Name[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), \"{0}\")] or ns:AlternativeName[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), \"{0}\")]]"
                            : "ns:Item[ns:Name[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), \"{0}\")]]";
                    break;
                }
            }
            XPathNodeIterator it = this.SelectNodes(String.Format(CultureInfo.InvariantCulture, xPath, termName.Trim().ToLower(CultureInfo.CurrentCulture)));

            EsdTermCollection terms = new EsdTermCollection();

            while (it.MoveNext())
            {
                terms.Add(this.GetEsdTermFromXml(it.Current));
            }

            terms.Sort();
            return(terms);
        }
        /// <summary>
        /// Gets the non-preferred terms for the specified ESD metadata preferred term
        /// </summary>
        /// <param name="preferredTermId">The id of the preferred term for which to get non-preferred terms</param>
        /// <returns>A collection of non-preferred terms</returns>
        public EsdTermCollection GetNonPreferredTerms(string preferredTermId)
        {
            string xPath = this.skosCompatible ? "ns:Item[@Id='{0}']/ns:AlternativeName" : "ns:Item[@Preferred='false' and ns:UseItem[@Id='{0}']]";

            xPath = String.Format(CultureInfo.InvariantCulture, xPath, preferredTermId.Trim());
            XPathNodeIterator it = this.SelectNodes(xPath);

            EsdTermCollection terms = new EsdTermCollection();

            while (it.MoveNext())
            {
                EsdTerm term = this.GetEsdTermFromXml(it.Current);
                terms.Add(term);
            }

            terms.Sort();
            return(terms);
        }
        /// <summary>
        /// Gets the child terms of the specified ESD metadata term
        /// </summary>
        /// <param name="termId">The id of the term to get</param>
        /// <returns>A collection of child terms</returns>
        public EsdTermCollection GetChildTerms(string termId)
        {
            // get child terms
            string            xPath = String.Format(CultureInfo.InvariantCulture, "ns:Item[ns:BroaderItem[@Id='{0}']]", termId.Trim());
            XPathNodeIterator it    = this.SelectNodes(xPath);

            EsdTermCollection terms = new EsdTermCollection();

            while (it.MoveNext())
            {
                EsdTerm term = this.GetEsdTermFromXml(it.Current);
                if (term != null)
                {
                    terms.Add(term);
                }
            }

            terms.Sort();
            return(terms);
        }
Exemple #6
0
        /// <summary>
        /// Gets the ESD metadata terms mapped to the specified term in another ESD controlled list
        /// </summary>
        /// <param name="esdListXml">The XML file of the ESD controlled list containing the mapped terms</param>
        /// <param name="termId">The id of the term for which to get mapped terms</param>
        /// <param name="mapForwards">If false, map from list B to list A, rather than list A to list B</param>
        /// <param name="termIdToExclude">The id of a term which should not be included even if it is mapped to the specified term</param>
        /// <returns>A collection of mapped terms</returns>
        public EsdTermCollection GetMappedTerms(EsdControlledList esdListXml, string termId, bool mapForwards, string termIdToExclude)
        {
            if (termId == null)
            {
                return(new EsdTermCollection());
            }

            // get mappings
            string xPath;

            if (mapForwards)
            {
                xPath = String.Format(CultureInfo.InvariantCulture, "ns:ItemMapping[ns:From/ns:Item[@Id='{0}']]", termId.Trim());
            }
            else
            {
                xPath = String.Format(CultureInfo.InvariantCulture, "ns:ItemMapping[ns:To/ns:Item[@Id='{0}']]", termId.Trim());
            }
            XPathNodeIterator it = this.SelectNodes(xPath);

            // get details of mapped terms
            EsdTermCollection terms = new EsdTermCollection();

            if (mapForwards)
            {
                if (termIdToExclude != null && termIdToExclude.Length > 0)
                {
                    xPath = String.Format(CultureInfo.InvariantCulture, "ns:To/ns:Item[@Id!='{0}']", termIdToExclude);
                }
                else
                {
                    xPath = "ns:To/ns:Item";
                }
            }
            else
            {
                if (termIdToExclude != null && termIdToExclude.Length > 0)
                {
                    xPath = String.Format(CultureInfo.InvariantCulture, "ns:From/ns:Item[@Id!='{0}']", termIdToExclude);
                }
                else
                {
                    xPath = "ns:From/ns:Item";
                }
            }
            while (it.MoveNext())
            {
                XPathNodeIterator mappingNode = this.SelectNodes(xPath, it.Current);

                if (mappingNode.Count == 1)
                {
                    mappingNode.MoveNext();
                    EsdTerm term = esdListXml.GetTerm(mappingNode.Current.GetAttribute("Id", ""), EsdPreferredState.Preferred);
                    if (term != null)
                    {
                        terms.Add(term);
                    }
                }
            }

            terms.Sort();
            return(terms);
        }