SQL XML Import: XQuery [value()]: “)” was expected

£可爱£侵袭症+ 提交于 2020-01-15 03:14:26

问题


I am trying to insert data into a table in SQL from XML data. The XML file was created from Microsoft Excel, which gives it this header:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">

I am using this query to parse it:

;WITH XMLNAMESPACES ('urn:schemas-microsoft-com:office:spreadsheet' as ss)

select X.value('(ss:Row/ss:Cell/ss:@Data)','varchar(max)')
from @allUsers.nodes('Workbook/Worksheet/Table') as T(X)

which parses for about a half a second and then gives me this error:

XQuery [value()]: ")" was expected.

The data within the XML being parsed contain phone numbers, some of which contain ( and ) e.g.:

   <Row ss:AutoFitHeight="0" ss:Height="30">
    <Cell ss:StyleID="s22"/>
    <Cell ss:StyleID="s24"><Data ss:Type="String">JohnSmith</Data></Cell>
    <Cell ss:StyleID="s24"><Data ss:Type="String">JohnSmith</Data></Cell>
    <Cell ss:StyleID="s24"><Data ss:Type="String">XYZ</Data></Cell>
    <Cell ss:StyleID="s24"><Data ss:Type="String">(555) 555-5555</Data></Cell>
    <Cell ss:StyleID="s22"/>
   </Row>

but I don't think that an open parentheses within the would cause a problem.

My question is, has anybody else encountered this error before, since I can't seem to find any help through an online search?

EDIT I think I may have been going in the wrong direction in this case. I have posted a new question here: Separating XML values with the same tags into different rows SQL Server


回答1:


You asked very similar questions. I took the information from both and built this working example. Be aware of the xmlns-namespace which must be declared as "DEFAULT":

Simplified your XML, but the idea should be OK...

DECLARE @allUsers XML=
'<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <Worksheet>
 <Table>
   <Row ss:AutoFitHeight="0" ss:Height="30">
    <Cell ss:StyleID="s22"/>
    <Cell ss:StyleID="s24"><Data ss:Type="String">Jane Doe</Data></Cell>
    <Cell ss:StyleID="s24"><Data ss:Type="String">JaneDoe</Data></Cell>
    <Cell ss:StyleID="s24"><Data ss:Type="String">XYZ</Data></Cell>
    <Cell ss:StyleID="s24"><Data ss:Type="String">(555) 555-5555</Data></Cell>
    <Cell ss:StyleID="s22"/>
   </Row>
   </Table>
 </Worksheet>   
</Workbook>';

;WITH XMLNAMESPACES ('urn:schemas-microsoft-com:office:spreadsheet' as ss
                     ,DEFAULT 'urn:schemas-microsoft-com:office:spreadsheet')
SELECT T.X.value('Cell[1]/Data[1]','varchar(max)') AS DontKnow1
      ,T.X.value('Cell[2]/Data[1]','varchar(max)') AS Name
      ,T.X.value('Cell[3]/Data[1]','varchar(max)') AS UserName
      ,T.X.value('Cell[4]/Data[1]','varchar(max)') AS DontKnow2
      ,T.X.value('Cell[5]/Data[1]','varchar(max)') AS Telephone
      ,T.X.value('Cell[6]/Data[1]','varchar(max)') AS DontKnow3
FROM @allUsers.nodes('/Workbook/Worksheet/Table/Row') as T(X)



回答2:


It's probably not the parenthesis in the phone number that causes the error, but your XQuery query, which I think has a syntax error.

The @ is not at the proper location, and may also need to go since Data is not an attribute but an element. You could try to change

select X.value('(ss:Row/ss:Cell/ss:@Data)','varchar(max)')
from @allUsers.nodes('Workbook/Worksheet/Table') as T(X)

to

select X.value('(ss:Row/ss:Cell/ss:Data)[1]','varchar(max)')
from @allUsers.nodes('ss:Workbook/ss:Worksheet/ss:Table') as T(X)


来源:https://stackoverflow.com/questions/33242024/sql-xml-import-xquery-value-was-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!