【问题标题】:Removing repeated and really redundant namespaces删除重复且真正冗余的命名空间
【发布时间】:2011-10-19 00:38:33
【问题描述】:

在下面的代码中这个语句;

declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd

重复了 6 次,使代码变得非常混乱且难以理解:

SELECT XW_PK, xw_ExeVersion, xw_enterpriseCode, xw_DatabaseCode, xw_CompanyCode,
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd";
    (pd:action/@name)[1]', 'varchar(100)') Name,
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd";
    (pd:action/@started)[1]', 'varchar(100)') [Started],
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd";
    (pd:action/@ended)[1]', 'varchar(100)') [Ended],
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd";
    (pd:action/@Elapsed)[1]', 'varchar(100)') Elapsed,
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd";
    (pd:action/@elapsedWithChildren)[1]', 'varchar(100)') elapsedWithChildren
    FROM   stmusage
        CROSS APPLY xw_rawData.nodes('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd";
        /pd:performanceMeasurement/pd:action') as Poodle(Love)

我想要做的是只声明一次命名空间并完成它。问题是每个 xqueries 都嵌入在一个字符串中——我不完全确定——但我多年来积累的经验给我的印象是这些字符串不会与每个字符串交互其他任何时间。

【问题讨论】:

    标签: sql xml tsql namespaces xquery


    【解决方案1】:

    我不知道您正在编写的编程语言,但在任何其他语言中,我都会编写一个函数,将字符串“name”或“started”作为参数 PPPPP 并生成

    Poodle.Love.value('声明命名空间d="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd"; (pd:action/@{PPPPP})[1]', 'varchar(100)')

    结果。

    【讨论】:

      【解决方案2】:
      SELECT 
                  XMLTable.actions.value('(@name)', 'varchar(100)') Name,
                  XMLTable.actions.value('(@started)', 'datetime') [Started],
                  XMLTable.actions.value('(@started)', 'datetime') + 
                  convert(float, replace (replace (XMLTable.actions.value('(@elapsedWithChildren)', 'varchar(100)'),'PT',''),'S','')) / (60 * 60 * 24) Ended, --dodgy floating point rounding errors
                  convert(float, replace (replace (XMLTable.actions.value('(@elapsed)', 'varchar(100)'),'PT',''),'S','')) Elapsed,
                  convert(float, replace (replace (XMLTable.actions.value('(@elapsedWithChildren)', 'varchar(100)'),'PT',''),'S','')) ElapsedWithChildren,
                  XMLTable.actions.value('@moduleId','varchar(100)') moduleid,
                  XMLTable.actions.value('@actionCount','int') actionCount,
                  XW_PK, XW_ExeVersion, XW_EnterpriseCode, XW_DatabaseCode, XW_CompanyCode, ExportType
                  FROM StmUsage
                      CROSS APPLY xw_rawData.nodes('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd";
                      /pd:performanceMeasurement/pd:action') XMLTable(actions)
                  cross join (select 'Elapsed' ExportType union select 'ElapsedWithChildren') ExportTypeAlias
      

      简单!

      【讨论】:

        猜你喜欢
        • 2012-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多