Class: Saxon::QName

Inherits:
Object
  • Object
show all
Defined in:
lib/saxon/qname.rb

Overview

Represents QNames

Defined Under Namespace

Classes: PrefixedStringWithoutNSURIError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s9_qname) ⇒ QName

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of QName.



97
98
99
# File 'lib/saxon/qname.rb', line 97

def initialize(s9_qname)
  @s9_qname = s9_qname
end

Class Method Details

.clark(clark_string) ⇒ Saxon::QName

Create a Saxon::QName from a Clark-notation string.

Clark-notation for QNames uses {} to delimit the namespace, so for a QName not in a namespace it’s simply local-name, and for one in a namespace it’s {example.org/ns}local-name

Parameters:

  • clark_string (String)

    A QName in Clark notation.

Returns:



14
15
16
17
# File 'lib/saxon/qname.rb', line 14

def self.clark(clark_string)
  s9_qname = Saxon::S9API::QName.fromClarkName(clark_string)
  new(s9_qname)
end

.create(opts = {}) ⇒ Saxon::QName

Create a Saxon::QName from prefix, uri, and local name options

Parameters:

  • opts (Hash) (defaults to: {})
  • [String] (Hash)

    a customizable set of options

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/saxon/qname.rb', line 39

def self.create(opts = {})
  prefix = opts[:prefix]
  uri = opts[:uri]
  begin
    local_name = opts.fetch(:local_name)
  rescue KeyError
    raise ArgumentError, "The :local_name option must be passed"
  end

  s9_qname = Saxon::S9API::QName.new(*[prefix, uri, local_name].compact)
  new(s9_qname)
end

.eqname(eqname_string) ⇒ Saxon::QName

Create a Saxon::QName from an Expanded QName string.

Expanded QNames uses Q{} to delimit the namespace, so for a QName not in a namespace it’s simply Q{}local-name (or local-name}), and for one in a namespace it’s Qhttp://example.org/nslocal-name+

Parameters:

  • eqname_string (String)

    A QName in Expanded QName notation.

Returns:



27
28
29
30
# File 'lib/saxon/qname.rb', line 27

def self.eqname(eqname_string)
  s9_qname = Saxon::S9API::QName.fromEQName(eqname_string)
  new(s9_qname)
end

.resolve(qname_or_string, namespaces = {}) ⇒ Saxon::QName

Resolve a QName string into a Saxon::QName.

If the arg is a Saxon::QName already, it just gets returned. If it’s an instance of the underlying Saxon Java QName, it’ll be wrapped into a Saxon::QName

If the arg is a string, it’s resolved by using resolve_qname_string

Parameters:

  • qname_or_string (String, Symbol, Saxon::QName)

    the qname to resolve

  • namespaces (Hash<String => String>) (defaults to: {})

    the set of namespaces as a hash of "prefix" => "namespace-uri"

Returns:



63
64
65
66
67
68
69
70
71
72
# File 'lib/saxon/qname.rb', line 63

def self.resolve(qname_or_string, namespaces = {})
  case qname_or_string
  when String, Symbol
    resolve_qname_string(qname_or_string, namespaces)
  when self
    qname_or_string
  when Saxon::S9API::QName
    new(qname_or_string)
  end
end

.resolve_qname_string(qname_string, namespaces = {}) ⇒ Saxon::QName

Resolve a QName string of the form “prefix:local-name” into a Saxon::QName by looking up the namespace URI in a hash of "prefix" => "namespace-uri"

Parameters:

  • qname_string (String, Symbol)

    the QName as a "prefix:local-name" string

  • namespaces (Hash<String => String>) (defaults to: {})

    the set of namespaces as a hash of "prefix" => "namespace-uri"

Returns:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/saxon/qname.rb', line 81

def self.resolve_qname_string(qname_string, namespaces = {})
  local_name, prefix = qname_string.to_s.split(':').reverse
  uri = nil

  if prefix
    uri = namespaces[prefix]
    raise self::PrefixedStringWithoutNSURIError.new(qname_string, prefix) if uri.nil?
  end

  create(prefix: prefix, uri: uri, local_name: local_name)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare this QName with another. They compare equal if they have same URI and local name. Prefix is ignored.

Parameters:

Returns:

  • (Boolean)

    whether the two compare equal



141
142
143
144
# File 'lib/saxon/qname.rb', line 141

def ==(other)
  return false unless other.is_a?(QName)
  s9_qname.equals(other.to_java)
end

#clarkString

Return a Clark notation representation of the QName:

"{http://ns.url}local-name"

Note that the prefix is lost in Clark notation.

Returns:

  • (String)

    The QName represented using Clark notation.



122
123
124
# File 'lib/saxon/qname.rb', line 122

def clark
  @s9_qname.getClarkName
end

#eqnameString

Return a Extended QName notation representation of the QName:

"Q{http://ns.url}local-name"

Note that the prefix is lost in EQName notation.

Returns:

  • (String)

    The QName represented using EQName notation.



132
133
134
# File 'lib/saxon/qname.rb', line 132

def eqname
  @s9_qname.getEQName
end

#hashObject

Compute a hash-code for this Saxon::QName.

Two QNamess with the same local name and URI will have the same hash code (and will compare using eql?).

See Also:

  • Object#hash


151
152
153
# File 'lib/saxon/qname.rb', line 151

def hash
  @hash ||= (local_name + uri).hash
end

#inspectObject

Returns a more detailed string representation of the object, showing prefix, uri, and local_name instance variables



171
172
173
# File 'lib/saxon/qname.rb', line 171

def inspect
  "<Saxon::QName @prefix=#{prefix} @uri=#{uri} @local_name=#{local_name}>"
end

#local_nameString

Returns The local name part of the QName.

Returns:

  • (String)

    The local name part of the QName



102
103
104
# File 'lib/saxon/qname.rb', line 102

def local_name
  @s9_qname.getLocalName
end

#prefixString

Returns The prefix part of the QName (” if unset).

Returns:

  • (String)

    The prefix part of the QName (” if unset)



107
108
109
# File 'lib/saxon/qname.rb', line 107

def prefix
  @s9_qname.getPrefix
end

#to_javaSaxon::S9API::QName

Returns the underlying Saxon QName object.

Returns:

  • (Saxon::S9API::QName)

    the underlying Saxon QName object



156
157
158
# File 'lib/saxon/qname.rb', line 156

def to_java
  s9_qname
end

#to_sObject

convert the QName to a lexical string, using the prefix (if there is one). So, Saxon::QName.clark('{http://example.org/#ns}hello').to_s returns 'hello', and Saxon::QName.create(prefix: 'pre', uri: 'http://example.org/#ns', local_name: 'hello').to_s returns 'pre:hello'



165
166
167
# File 'lib/saxon/qname.rb', line 165

def to_s
  s9_qname.to_s
end

#uriString

Returns The namespace URI part of the QName (” if unset).

Returns:

  • (String)

    The namespace URI part of the QName (” if unset)



112
113
114
# File 'lib/saxon/qname.rb', line 112

def uri
  @s9_qname.getNamespaceURI
end