From ec1ab80cf9a84bc373aeb589f6978bdeb62db73e Mon Sep 17 00:00:00 2001 From: PharmCat <13901158+PharmCat@users.noreply.github.com> Date: Fri, 24 Mar 2023 20:31:03 +0300 Subject: [PATCH] spss labels from question or description --- Project.toml | 2 +- src/spss.jl | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index c7ee55c..3b49c38 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ODMXMLTools" uuid = "2456a17b-6ca2-4f51-9342-f0287e829718" authors = ["PharmCat "] -version = "0.6.0" +version = "0.6.1" [deps] diff --git a/src/spss.jl b/src/spss.jl index e273c09..c5947dd 100644 --- a/src/spss.jl +++ b/src/spss.jl @@ -41,9 +41,22 @@ function Base.show(io::IO, spssc::SPSSValueLabels) print(io, ".") end +function getTTcontent(node, lang::String) + tt = findelements(node, :TranslatedText) + li = findfirst(x-> attribute(x, :lang) == lang, tt) + if isnothing(li) + return content(first(tt)) + else + return attribute(df[i], variable) => content(tt[li]) + end +end +function getTTcontent(node, ::Nothing) + tt = findelement(node, :TranslatedText) + return content(tt) +end """ - spss_form_variable_labels(mdb, form; variable = :OID, labels = :Name) + spss_form_variable_labels(mdb, form; variable = :OID, labels = :Name, source = :attr, lang = nothing) SPSS command to set variable labels. @@ -51,12 +64,34 @@ SPSS command to set variable labels. `labels` - labels names attribute, `Name` by default. +If `source` == `:Question` - try to get description from `TranslatedText` of `Question` element, if there is no `Question` element - get from attribute `labels`. + +If `source` == `:Description` - try to get description from `TranslatedText` of `Description` element. + """ -function spss_form_variable_labels(mdb, form; variable = :OID, labels = :Name) +function spss_form_variable_labels(mdb, form; variable = :OID, labels = :Name, source = :attr, lang = nothing) df = itemformdefcontent_(mdb, form; optional = true) v = Vector{Pair}(undef, length(df)) for i = 1:size(df, 1) - v[i] = attribute(df[i], variable) => attribute(df[i], labels) + if source == :attr + v[i] = attribute(df[i], variable) => attribute(df[i], labels) + elseif source == :Question + q = findelement(df[i], :Question) + if isnothing(q) + v[i] = attribute(df[i], variable) => attribute(df[i], labels) + else + v[i] = attribute(df[i], variable) => getTTcontent(q, lang) + end + elseif source == :Description + q = findelement(df[i], :Description) + if isnothing(q) + v[i] = attribute(df[i], variable) => attribute(df[i], labels) + else + v[i] = attribute(df[i], variable) => getTTcontent(q, lang) + end + else + v[i] = attribute(df[i], variable) => attribute(df[i], labels) + end end SPSSVariableLabels(v) end