sql-server - 使用通配符的 T-SQL xquery .modify 方法

标签 sql-server xml t-sql xquery xml-dml

我正在使用 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 和 StackOverFlowed 上进行了详细的搜索,但也许是因为我在这方面缺乏经验,所以我没有正确地提出问题。

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

最佳答案

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

无论如何,比一遍又一遍地编写声明 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;

关于sql-server - 使用通配符的 T-SQL xquery .modify 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36898730/

相关文章:

c# - 是否有理由在 c# 中使用 clausule 检查多个内部的 null?

SQL SERVER 2008 的 SQL 查询

.net - 在数据库中存储增量而不是整个对象

python - 无法使用 django-pyodbc 从 django 查询 SQL Server

javascript - 是否可以使用 XPath 获取此结果集?

sql-server-2008 - 有没有办法让 INSTEAD OF 触发器与 SCOPE_IDENTITY 一起工作?

sql - 事务中不再有可用的锁类别

xml - DTD 中的外部实体和符号是什么?

javascript - 如何使用 javascript 检索带有条件 if() 的选择标记中指定值的 xml 数据

sql - 删除重复行时更新具有特定 id 的表行