초보자 용 질문입니다. 잘하면 쉽게 대답 할 수 있습니다. SQLServer 2008로로드하고 유용한 정보를 추출 할 XML 파일이 있습니다.SQLServer : 쿼리 데이터에 대한 액세스를 차단하는 네임 스페이스
저는 간단히 시작하여 이름 (\ gpx \ name)을 추출하려고합니다. 내가 가진 코드는 다음과 같습니다 처음 두 행 단지로 대체하는 경우
<?xml version="1.0" encoding="utf-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0" creator="Groundspeak Pocket Query" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0 http://www.groundspeak.com/cache/1/0/cache.xsd" xmlns="http://www.topografix.com/GPX/1/0">
<name>EM</name>
<desc>Geocache file generated by Groundspeak</desc>
<author>Groundspeak</author>
<email>[email protected]</email>
<time>2010-03-24T14:01:36.4931342Z</time>
<keywords>cache, geocache, groundspeak</keywords>
<wpt lat="51.2586" lon="-2.213067">
<time>2008-03-30T07:00:00Z</time>
<name>GC1APHM</name>
<desc>Sandman's Noble Hoard by Sandman1973, Unknown Cache (2/3)</desc>
<groundspeak:cache id="832000" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0">
<groundspeak:name>Sandman's Noble Hoard</groundspeak:name>
<groundspeak:placed_by>Sandman1973</groundspeak:placed_by>
</groundspeak:cache>
</wpt>
</gpx>
:
다음DECLARE @x xml; SELECT @x = xCol.BulkColumn FROM OPENROWSET (BULK 'C:\Data\EM.gpx', SINGLE_BLOB) AS xCol; -- confirm the xml data is in @x select @x as XML_Data -- try and get the name of the gpx section SELECT c.value('name[1]', 'varchar(200)') as Name from @x.nodes('gpx') x(c)
XML 파일의 크게 단축 버전입니다
<gpx>
위를 예를 제대로 작동하지만 다음 groundspeak : name (/ gpx/wpt/groundspeak : cache/groundspeak : name)에 액세스 할 수 없으므로 내 추측에 네임 스페이스 문제가 있습니다.
도움이 될 것입니다.
감사합니다. 귀하가 게시 한 내용은 WITH XMLNAMESPACES 솔루션에서 제대로 작동하지 않았습니다. 그러나 다시 얻은 것은 모두 NULL이었습니다. 그러나 이름 공간 [1]이 필요 했으므로 이름 공간 참조가 필요했습니다. ; WITH XMLNAMESPACES ('http : //www.topografix.com/GPX/1/0 'as ns1) SELECT c.value ('ns1 : name [1] ','varchar (200) ') 이름으로 FROM @ x.nodes ('ns1 : gpx ') x (c) – Brian
두 번째 쿼리는 여전히 작동하지 않습니다. 시작하기 위해서는 모범 사례에서 모험 작품을 참조하십시오 - 쉽게 고칠 수 있지만 여전히 기쁨은 없습니다. 으로 변경 이름 FROM @ x.nodes ('declare namespace ns1 = "http://www.topografix.com/GPX/")로 c.value ('name [1] ','varchar (200) 1/0 "; ns1 : gpx ') x (c) 은 NULL을 포함하는 레코드 하나를 반환합니다. 이름 [1]을 (를) 변경하려고하면 오류가 발생합니다. – Brian
OK, 나는 직장 답을 수정했다. 동의 해주세요. – erikkallen