【问题标题】:T-SQL xquery .modify method using a wildcard使用通配符的 T-SQL xquery .modify 方法
【发布时间】:2016-08-22 06:38:51
【问题描述】:

我在 SQL Server 2014 中工作。我创建了一个存储过程来处理它,最后,获取最终查询输出并将其格式化为 XML。由于过程中逻辑的性质,有时必须从最终的 XML 输出中删除一个节点。这是 XML 输出的示例(为简洁起见,我没有包括根节点;希望它们不会被要求回答我的问题):

<DALink>
    <InteractingIngredients>
        <InteractingIngredient>
            <IngredientID>1156</IngredientID>
            <IngredientDesc>Lactobacillus acidophilus</IngredientDesc>
            <HICRoot>Lactobacillus acidophilus</HICRoot>
            <PotentiallyInactive>Not necessary</PotentiallyInactive>
            <StatusCode>Live</StatusCode>
        </InteractingIngredient>
    </InteractingIngredients>
    <ActiveProductsExistenceCode>Exist</ActiveProductsExistenceCode>
    <IngredientTypeBasisCode>1</IngredientTypeBasisCode>
    <AllergenMatch>Lactobacillus acidophilus</AllergenMatch>
    <AllergenMatchType>Ingredient</AllergenMatchType>
</DALink>
<ScreenDrug>
    <DrugID>1112894</DrugID>
    <DrugConceptType>RxNorm_SemanticClinicalDr</DrugConceptType>
    <DrugDesc>RxNorm LACTOBACILLUS ACIDOPHILUS/PECTIN ORAL capsule</DrugDesc>
    <Prospective>true</Prospective>
</ScreenDrug>

在该过程中,我的代码查看了上面的 XML 结构,并在它不应该存在时删除了一个节点,因为修改 xml 比调整查询输出更容易。这是该代码的示例:

SET @xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="OneThing"]');
SET @xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="AnotherThing"]');
SET @xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="SomethingElse"]');
SET @xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="SomethingElseAgain"]');
SET @xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="RxNorm*"]');

最后一个命令是我不知道如何工作的命令。我需要做的就是寻找元素“DrugConceptType”以字符串“RxNorm”开头的实例,因为可能出现多个版本的字符串。

我已经用 Google 和 StackOverFlow 进行了详细搜索,但可能是因为我在这方面缺乏经验,所以我没有正确地提出问题。

有没有一种相对简单的方法来重写上面的最终 .modify 语句以在“RxNorm”之后使用通配符?

【问题讨论】:

    标签: sql-server xml tsql xquery xml-dml


    【解决方案1】:

    根节点的减少确实是个问题,因为您使用的是命名空间“ccxsd”,而您的 XML 没有显示这一点。

    无论如何,总比一遍又一遍地写declare namespace ...,这是你声明的第一行:

    ;WITH XMLNAMESPACES('http://schemas.fdbhealth.com/CC/v1_3' AS ccxsd)
    

    好吧,因为.modify() 是一个一个语句调用,它并没有那么大的区别......

    但是对于你的通配符

    的问题

    这将删除元素内容以“RxNorm”开头的所有节点:

    SET @xml.modify('delete //*[fn:substring(.,1,6)="RxNorm"]');
    

    注意缺少命名空间...无法测试...

    编辑:一个简化的工作示例:

    您必须使用您的实际 XML(使用根目录和命名空间)检查这一点

    DECLARE @xml_Out XML=
    '<DALink>
        <InteractingIngredients>
            <InteractingIngredient>
                <IngredientID>1156</IngredientID>
                <IngredientDesc>Lactobacillus acidophilus</IngredientDesc>
                <HICRoot>Lactobacillus acidophilus</HICRoot>
                <PotentiallyInactive>Not necessary</PotentiallyInactive>
                <StatusCode>Live</StatusCode>
            </InteractingIngredient>
        </InteractingIngredients>
        <ActiveProductsExistenceCode>Exist</ActiveProductsExistenceCode>
        <IngredientTypeBasisCode>1</IngredientTypeBasisCode>
        <AllergenMatch>Lactobacillus acidophilus</AllergenMatch>
        <AllergenMatchType>Ingredient</AllergenMatchType>
    </DALink>
    <ScreenDrug>
        <DrugID>1112894</DrugID>
        <DrugConceptType>RxNorm_SemanticClinicalDr</DrugConceptType>
        <DrugDesc>RxNorm LACTOBACILLUS ACIDOPHILUS/PECTIN ORAL capsule</DrugDesc>
        <Prospective>true</Prospective>
    </ScreenDrug>';
    
    SET @xml_Out.modify('delete //InteractingIngredients[fn:substring((../../ScreenDrug/DrugConceptType)[1],1,6)="RxNorm"]');
    
    SELECT @xml_Out;
    

    【讨论】:

    • 先生。 @Shnugo,您的解决方案就像一个冠军。这是我正在寻找的函数,但我无法弄清楚如何正确构建语法以使其工作。这是我希望得到的答案,我认为这个问题已经得到解答。
    • @joe,很高兴看到这篇文章。当您认为此问题已回答时,请您在投票柜台下方勾选接受检查。谢谢
    猜你喜欢
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多