Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理sql中xml标记之间的前导空格_Sql_Sql Server_Xml - Fatal编程技术网

如何处理sql中xml标记之间的前导空格

如何处理sql中xml标记之间的前导空格,sql,sql-server,xml,Sql,Sql Server,Xml,我的SQL表列包含如下xml数据 我试图根据特定的标记值(年龄)查找元素。但是,我的xml值具有前导空格。因此,它在这个条件下失败了。请引导我处理这件事 例如: declare @age2 varchar(40)='23' declare @ageNew2 varchar(40)='43' declare @xmldata xml='<Root> <Identification Name="John" Family="Brown"><Age>

我的SQL表列包含如下xml数据

我试图根据特定的标记值(年龄)查找元素。但是,我的xml值具有前导空格。因此,它在这个条件下失败了。请引导我处理这件事

例如:

declare @age2 varchar(40)='23'
declare @ageNew2 varchar(40)='43'
declare @xmldata xml='<Root>
         <Identification Name="John"  Family="Brown"><Age>40</Age><name>xxx</name></Identification>
         <Identification Name="Smith" Family="Johnson"><Age>35</Age><name>xxx</name></Identification>
         <Identification Name="Jessy" Family="Albert"><Age>60</Age><name>xxx</name></Identification>
         <Identification Name="Mike"  Family="Brown"><Age>23</Age><name>xxx</name></Identification>
         <Identification Name="Sarah" Family="Johnson"><Age>40</Age><name>xxx</name></Identification>
         <Identification Name="Jessy" Family="Albert"><Age>40</Age><name>xxx</name></Identification>
         <Identification Name="Mike"  Family="Brown"><Age>23 </Age><name>xxx</name></Identification>
         <Identification Name="Sarah" Family="Johnson"><Age>45</Age><name>xxx</name></Identification>         
         </Root>'

declare @dynamicData varchar(max)


set @dynamicData=' declare @xml varchar(max) ='''+Convert(varchar(max),@xmldata)+''' '
set @dynamicData +=' declare @xmld xml=convert(xml,@xml) '

set @dynamicData +=' declare @age varchar(55) ='''+@age2+''''
set @dynamicData +=' declare @ageNew varchar(55) ='''+@ageNew2+''''


DECLARE @nodeCount int
DECLARE @i int

SET @i = 0


SELECT @nodeCount = @xmldata.value('count(/Root/Identification/Age)','int')

WHILE (@i < @nodeCount)
BEGIN

set @dynamicData+=' set @xmld.modify(''replace value of (/Root/Identification[Age=sql:variable("@age")]/name/text())['+convert(varchar(10),@i)+'] with "'+@ageNew2+'"'') '

SET @i = @i + 1
END 
set @dynamicData+=' select convert(xml,@xmld) '

exec(@dynamicData)
declare@age2 varchar(40)='23'
声明@ageNew2 varchar(40)='43'
声明@xmldataxml='0
40xxx
35xxx
60xxx
23xxx
40xxx
40xxx
23 xxx
45xxx
'
声明@dynamicatavarchar(最大值)
set@dynamicData='declare@xml-varchar(max)='''+Convert(varchar(max),@xmldata)+''
set@dynamicData+=“declare@xmldxml=convert(xml,@xml)”
set@dynamicData+='声明@age varchar(55)=''+@age2+''
set@dynamicData+='声明@ageNew varchar(55)=''+@ageNew2+''
声明@nodeCount int
声明@i int
设置@i=0
选择@nodeCount=@xmldata.value('count(/Root/Identification/Age)','int')
而(@i<@nodeCount)
开始
set@dynamicData+='set@xmld.modify(''将(/Root/Identification[Age=sql:variable(“@Age”)]/name/text()的值['+convert(varchar(10),@i)+']替换为“+@ageNew2+”)”
设置@i=@i+1
结束
set@dynamicData+=“选择转换(xml,@xmld)”
exec(@dynamicata)
在这里,我想把23岁的年龄更新到43岁。所以,按照我的期望。它应该取代两个地方。但是,它仅仅取代了第一个。因为,第二个有空间。
所以,请引导我。谢谢

如果搜索变量是整数,那么它就工作了。 因为它从隐式转换中获益

首先将其转换为INT变量,然后使用它

示例代码片段:

DECLARE @xmldata XML = N'<Root>
         <Identification Name="John"  Family="Brown"><Age>40</Age><name>xxx</name></Identification>
         <Identification Name="Smith" Family="Johnson"><Age>35</Age><name>xxx</name></Identification>
         <Identification Name="Jessy" Family="Albert"><Age>60</Age><name>xxx</name></Identification>
         <Identification Name="Mike"  Family="Brown"><Age>23</Age><name>xxx</name></Identification>
         <Identification Name="Sarah" Family="Johnson"><Age>40</Age><name>xxx</name></Identification>
         <Identification Name="Jessy" Family="Albert"><Age>40</Age><name>xxx</name></Identification>
         <Identification Name="Mike"  Family="Brown"><Age>23 </Age><name>xxx</name></Identification>
         <Identification Name="Sarah" Family="Johnson"><Age>45</Age><name>xxx</name></Identification>         
         </Root>';

DECLARE @age2 VARCHAR(40)='23';
DECLARE @ageNew2 VARCHAR(40)='43';

--
-- let us use an int to search
--
DECLARE @age2int INT = cast(@age2 as int);

--
-- how many to modify
--
DECLARE @cnt int = @xmldata.value('count(/Root/Identification/Age[.=sql:variable("@age2int")])','int');

--
-- loop over the elements to modify
--
WHILE @cnt > 0
BEGIN
  SET @xmldata.modify('replace value of (/Root/Identification/Age[.=sql:variable("@age2int")]/text())[1] with sql:variable("@ageNew2")');
  SET @cnt = @cnt - 1;
END;
DECLARE@xmldataxml=N'

如果搜索变量是整数,则它可以工作。 因为它从隐式转换中获益

首先将其转换为INT变量,然后使用它

示例代码片段:

DECLARE @xmldata XML = N'<Root>
         <Identification Name="John"  Family="Brown"><Age>40</Age><name>xxx</name></Identification>
         <Identification Name="Smith" Family="Johnson"><Age>35</Age><name>xxx</name></Identification>
         <Identification Name="Jessy" Family="Albert"><Age>60</Age><name>xxx</name></Identification>
         <Identification Name="Mike"  Family="Brown"><Age>23</Age><name>xxx</name></Identification>
         <Identification Name="Sarah" Family="Johnson"><Age>40</Age><name>xxx</name></Identification>
         <Identification Name="Jessy" Family="Albert"><Age>40</Age><name>xxx</name></Identification>
         <Identification Name="Mike"  Family="Brown"><Age>23 </Age><name>xxx</name></Identification>
         <Identification Name="Sarah" Family="Johnson"><Age>45</Age><name>xxx</name></Identification>         
         </Root>';

DECLARE @age2 VARCHAR(40)='23';
DECLARE @ageNew2 VARCHAR(40)='43';

--
-- let us use an int to search
--
DECLARE @age2int INT = cast(@age2 as int);

--
-- how many to modify
--
DECLARE @cnt int = @xmldata.value('count(/Root/Identification/Age[.=sql:variable("@age2int")])','int');

--
-- loop over the elements to modify
--
WHILE @cnt > 0
BEGIN
  SET @xmldata.modify('replace value of (/Root/Identification/Age[.=sql:variable("@age2int")]/text())[1] with sql:variable("@ageNew2")');
  SET @cnt = @cnt - 1;
END;
DECLARE@xmldataxml=N'

您可以尝试将Age/text()强制转换为token以删除所有空白(如果这是您真正需要的)


替换(/Root/Identification[xs:token(Age[1])=sql:variable(@Age”)]/name/text())

您可以尝试将Age/text()强制转换为token以删除所有空白(如果这是您真正需要的)


替换(/Root/Identification[xs:token(Age[1])=sql:variable(@Age”)]/name/text()的值。

请将您的问题包含在示例数据和所需结果中。请查看endswith的解决方法,特别是help@ZoharPeled请查收。我已经更新了您的问题,包括样本数据和期望的结果。请看一下,特别是endswith的解决方法help@ZoharPeled请查收。我已经更新了ID是字母数字而不是整数。它的varchar@NatarajanVeerasekaran你能在你的问题中加一个例子吗?文本很好。T-sql代码也很好。好的,但我想数据应该有id?但你似乎试图改变所有23岁的人。对吗?@NatarajanVeerasekaran有点晚了,但是更新了。我很喜欢另一个答案的技巧。id是字母数字而不是整数。它的varchar@NatarajanVeerasekaran你能在你的问题中加一个例子吗?文本很好。T-sql代码也很好。好的,但我想数据应该有id?但你似乎试图改变所有23岁的人。对吗?@NatarajanVeerasekaran有点晚了,但是更新了。我确实喜欢另一个答案的技巧。好答案,+1来自我这边!巧妙地使用数据类型转换来模拟SQL Server中的缺失
normalized-space()
function回答得好,+1来自我这边!智能使用数据类型转换来模拟SQL Server
normalized-space()函数中的缺失