{"id":31404,"date":"2018-04-21T15:47:46","date_gmt":"2018-04-21T15:47:46","guid":{"rendered":"https:\/\/ampscript.guide\/buildrowsetfromxml\/"},"modified":"2021-04-06T01:18:23","modified_gmt":"2021-04-06T01:18:23","slug":"buildrowsetfromxml","status":"publish","type":"post","link":"https:\/\/ampscript.guide\/buildrowsetfromxml\/","title":{"rendered":"BuildRowSetFromXML"},"content":{"rendered":"<h2>BuildRowSetFromXML<\/h2>\n<p>This function creates a row set from an XML string.  It returns an error or an empty row set if no XML matches the specified XPATH.<\/p>\n<h3>Arguments<\/h3>\n<p><code>BuildRowSetFromXML(1,2,3)<\/code><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center\">Ordinal<\/th>\n<th style=\"text-align: left\">Type<\/th>\n<th style=\"text-align: left\">Required<\/th>\n<th style=\"text-align: left\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center\">1<\/td>\n<td style=\"text-align: left\">String<\/td>\n<td style=\"text-align: left\">True<\/td>\n<td style=\"text-align: left\">Source XML string to parse<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">2<\/td>\n<td style=\"text-align: left\">String<\/td>\n<td style=\"text-align: left\">True<\/td>\n<td style=\"text-align: left\">XPATH string that specifies the root XML node from which to build the row set<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">3<\/td>\n<td style=\"text-align: left\">Boolean<\/td>\n<td style=\"text-align: left\">True<\/td>\n<td style=\"text-align: left\">Return an empty row set if XPATH is not found in source string<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p>NOTE: This function supports the <a href=\"https:\/\/www.w3.org\/TR\/1999\/REC-xpath-19991116\/\">XPATH 1.0 specification<\/a>.<\/p>\n<p>NOTE: The following nodes will return no value:<\/p>\n<\/blockquote>\n<ul>\n<li>CDATA<\/li>\n<li>Comment<\/li>\n<li>Document<\/li>\n<li>Document Fragments<\/li>\n<li>DocumentType<\/li>\n<li>Entities<\/li>\n<li>Entity References<\/li>\n<li>Notation<\/li>\n<li>ProcessingInformation<\/li>\n<li>Whitespace<\/li>\n<li>XmlDeclaration<\/li>\n<\/ul>\n<h3>Example 1<\/h3>\n<p>The following example retrieves XML node values:<\/p>\n<pre><code>%%[\n\nvar @xml, @isXML, @nodes, @rowCount\n\nset @xml = \"\"\nset @xml = concat(@xml, '&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;')\nset @xml = concat(@xml, '&lt;cart&gt;')\nset @xml = concat(@xml, '  &lt;items&gt;')\nset @xml = concat(@xml, '    &lt;item&gt;')\nset @xml = concat(@xml, '       &lt;sku&gt;&lt;![CDATA[123]]&gt;&lt;\/sku&gt;')\nset @xml = concat(@xml, '       &lt;name&gt;&lt;![CDATA[Square]]&gt;&lt;\/name&gt;')\nset @xml = concat(@xml, '       &lt;url&gt;&lt;![CDATA[https:\/\/limedash.com?sku=123]]&gt;&lt;\/url&gt;')\nset @xml = concat(@xml, '    &lt;\/item&gt;')\nset @xml = concat(@xml, '    &lt;item&gt;')\nset @xml = concat(@xml, '        &lt;sku&gt;&lt;![CDATA[456]]&gt;&lt;\/sku&gt;')\nset @xml = concat(@xml, '        &lt;name&gt;&lt;![CDATA[Circle]]&gt;&lt;\/name&gt;')\nset @xml = concat(@xml, '        &lt;url&gt;&lt;![CDATA[https:\/\/limedash.com?sku=456]]&gt;&lt;\/url&gt;')\nset @xml = concat(@xml, '    &lt;\/item&gt;')\nset @xml = concat(@xml, '    &lt;item&gt;')\nset @xml = concat(@xml, '        &lt;sku&gt;&lt;![CDATA[789]]&gt;&lt;\/sku&gt;')\nset @xml = concat(@xml, '        &lt;name&gt;&lt;![CDATA[Triangle]]&gt;&lt;\/name&gt;')\nset @xml = concat(@xml, '        &lt;url&gt;&lt;![CDATA[https:\/\/limedash.com?sku=789]]&gt;&lt;\/url&gt;')\nset @xml = concat(@xml, '    &lt;\/item&gt;')\nset @xml = concat(@xml, '  &lt;\/items&gt;')\nset @xml = concat(@xml, '&lt;\/cart&gt; ')\n\nif indexOf(@xml,\"&lt;cart&gt;\") &gt; 0 then\n\n  set @nodes = BuildRowsetFromXML(@xml,\"\/cart\/items\/item\",0)\n  set @rowCount = rowcount(@nodes)\n\n  if @rowCount &gt; 0 then\n\n    for @i = 1 to @rowCount do\n\n      var @sku\n      var @name\n      var @url\n\n      set @nodepath = concat(\"\/cart\/items\/item[\",@i,\"]\/\")\n\n      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,\"sku\"))) &gt; 0 then\n          set @sku = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,\"sku\"),0),1),'Value')\n      endif\n\n      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,\"name\"))) &gt; 0 then\n          set @name = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,\"name\"),0),1),'Value')\n      endif\n\n      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,\"url\"))) &gt; 0 then\n          set @url = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,\"url\"),0),1),'Value')\n      endif\n\n      if not empty(@sku) and not empty(@name) and not empty(@url) then\n\n      ]%%\n\n       &lt;br&gt;&lt;a href=\"%%=redirectto(@url)=%%\"&gt;%%=v(@name)=%% (%%=v(@sku)=%%)&lt;\/a&gt;\n\n      %%[\n\n      endif\n\n    next @i\n\n  else\n\n   output(concat(\"&lt;br&gt;no products found\"))\n\n  endif\n\nelse\n\n  output(concat(\"&lt;br&gt;no XML found\"))\n\nendif\n\n]%%<\/code><\/pre>\n<h4>Output<\/h4>\n<pre><code>&lt;br&gt;&lt;a href=\"https:\/\/limedash.com\/?sku=123\"&gt;Square (123)&lt;\/a&gt;\n&lt;br&gt;&lt;a href=\"https:\/\/limedash.com\/?sku=456\"&gt;Circle (456)&lt;\/a&gt;\n&lt;br&gt;&lt;a href=\"https:\/\/limedash.com\/?sku=789\"&gt;Triangle (789)&lt;\/a&gt;<\/code><\/pre>\n<h3>Example 2<\/h3>\n<p>The following example retrieves values from the XML node attributes:<\/p>\n<pre><code>%%[\n\nvar @xml, @isXML, @nodes, @rowCount\n\nset @xml = \"\"\nset @xml = concat(@xml, '&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;')\nset @xml = concat(@xml, '&lt;cart&gt;')\nset @xml = concat(@xml, '  &lt;item sku=\"123\" url=\"https:\/\/limedash.com?sku=123\"&gt;Square&lt;\/item&gt;')\nset @xml = concat(@xml, '  &lt;item sku=\"246\" url=\"https:\/\/limedash.com?sku=246\"&gt;Circle&lt;\/item&gt;')\nset @xml = concat(@xml, '  &lt;item sku=\"789\" url=\"https:\/\/limedash.com?sku=789\"&gt;Triangle&lt;\/item&gt;')\nset @xml = concat(@xml, '&lt;\/cart&gt; ')\n\nif indexOf(@xml,\"&lt;cart&gt;\") &gt; 0 then\n\n  set @nodes = BuildRowsetFromXML(@xml,\"\/cart\/item\",0)\n  set @rowCount = rowcount(@nodes)\n\n  if @rowCount &gt; 0 then\n\n    for @i = 1 to @rowCount do\n\n      var @sku\n      var @name\n      var @url\n\n      set @nodepath = concat(\"\/cart\/item[\",@i,\"]\/\")\n\n      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,\"@sku\"))) &gt; 0 then\n          set @sku = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,\"@sku\"),0),1),'Value')\n      endif\n\n      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,\"@url\"))) &gt; 0 then\n          set @url = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,\"@url\"),0),1),'Value')\n      endif\n\n      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,\"text()\"))) &gt; 0 then\n          set @name = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,\"text()\"),0),1),'Value')\n      endif\n\n      if not empty(@sku) and not empty(@name) and not empty(@url) then\n\n      ]%%\n\n       &lt;br&gt;&lt;a href=\"%%=redirectto(@url)=%%\"&gt;%%=v(@name)=%% (%%=v(@sku)=%%)&lt;\/a&gt;\n\n      %%[\n\n      endif\n\n    next @i\n\n  else\n\n   output(concat(\"&lt;br&gt;no products found\"))\n\n  endif\n\nelse\n\n  output(concat(\"&lt;br&gt;no XML found\"))\n\nendif\n\n]%%<\/code><\/pre>\n<h4>Output<\/h4>\n<pre><code>&lt;br&gt;&lt;a href=\"https:\/\/limedash.com\/?sku=123\"&gt;Square (123)&lt;\/a&gt;\n&lt;br&gt;&lt;a href=\"https:\/\/limedash.com\/?sku=456\"&gt;Circle (456)&lt;\/a&gt;\n&lt;br&gt;&lt;a href=\"https:\/\/limedash.com\/?sku=789\"&gt;Triangle (789)&lt;\/a&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>BuildRowSetFromXML This function creates a row set from an XML string. It returns an error or an empty row set if no XML matches the specified XPATH. Arguments BuildRowSetFromXML(1,2,3) Ordinal Type Required Description 1 String True Source XML string to parse 2 String True XPATH string that specifies the root XML node from which to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v14.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The AMPscript Guide - BuildRowSetFromXML<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ampscript.guide\/buildrowsetfromxml\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The AMPscript Guide - BuildRowSetFromXML\" \/>\n<meta property=\"og:description\" content=\"BuildRowSetFromXML This function creates a row set from an XML string. It returns an error or an empty row set if no XML matches the specified XPATH. Arguments BuildRowSetFromXML(1,2,3) Ordinal Type Required Description 1 String True Source XML string to parse 2 String True XPATH string that specifies the root XML node from which to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ampscript.guide\/buildrowsetfromxml\/\" \/>\n<meta property=\"og:site_name\" content=\"The AMPscript Guide\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-21T15:47:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-06T01:18:23+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ampscript.guide\/#website\",\"url\":\"https:\/\/ampscript.guide\/\",\"name\":\"The AMPscript Guide\",\"description\":\"The Definitive Scripting Manual for Salesforce Marketing Cloud\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/ampscript.guide\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/buildrowsetfromxml\/#webpage\",\"url\":\"https:\/\/ampscript.guide\/buildrowsetfromxml\/\",\"name\":\"The AMPscript Guide - BuildRowSetFromXML\",\"isPartOf\":{\"@id\":\"https:\/\/ampscript.guide\/#website\"},\"datePublished\":\"2018-04-21T15:47:46+00:00\",\"dateModified\":\"2021-04-06T01:18:23+00:00\",\"author\":{\"@id\":\"https:\/\/ampscript.guide\/#\/schema\/person\/5335042f77731e84f9808aecef25daec\"},\"breadcrumb\":{\"@id\":\"https:\/\/ampscript.guide\/buildrowsetfromxml\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ampscript.guide\/buildrowsetfromxml\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ampscript.guide\/buildrowsetfromxml\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/\",\"url\":\"https:\/\/ampscript.guide\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/buildrowsetfromxml\/\",\"url\":\"https:\/\/ampscript.guide\/buildrowsetfromxml\/\",\"name\":\"BuildRowSetFromXML\"}}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/ampscript.guide\/#\/schema\/person\/5335042f77731e84f9808aecef25daec\",\"name\":\"dev\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","_links":{"self":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31404"}],"collection":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/comments?post=31404"}],"version-history":[{"count":0,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31404\/revisions"}],"wp:attachment":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/media?parent=31404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/categories?post=31404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/tags?post=31404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}