views:

794

answers:

1

How do I select the inner text of an XML node using XQuery?

Microsoft Books Online shows how to retrive an attribute below:

DECLARE @myDoc xml
DECLARE @ProdID int
SET @myDoc = '<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
  <Warranty>1 year parts and labor</Warranty>
  <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>'

SET @ProdID =  @myDoc.value('(/Root/ProductDescription/@ProductID)[1]', 'int' )
SELECT @ProdID

How would I get the inner text value of the Warranty node?

+2  A: 

Something like this:

DECLARE @Warranty VARCHAR(50)

SET @Warranty = @myDoc.value('(/Root/Features/Warranty/text())[1]', 'varchar(50)' )

SELECT @Warranty

Marc

marc_s