1

我在 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”之后使用通配符?

4

1 回答 1

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;
于 2016-04-27T20:57:36.047 回答