Example: XSLT_SHRED_BATCH Combined <xsl:copy-of select=""> and <xsl:value-of select=""> - Teradata Vantage NewSQL Engine - 16.20

Teradata Vantageā„¢ XML Data Type

prodname
Teradata Database
Teradata Vantage NewSQL Engine
vrm_release
16.20
category
Programming Reference
featnum
B035-1140-162K

These examples show how to use both the <xsl:copy-of select=""> and <xsl:value-of select=""> mapping definitions.

Before running the example, ensure the test user is created with the required permissions and the required tables are created. See Setting Up the XSLT_SHRED_BATCH and XSLT_SHRED Examples for details.

Populate the staging table used to store the XML source document.

DELETE Offer;
DELETE Input_Docs;

INSERT INTO Input_Docs VALUES(1, CREATEXML('<?xml version="1.0"?>
<Root>
 <predictixOfferMessage>
   <Offer><offerid>1000001</offerid></Offer>
   <mediaBlock><mediaBlockid>90000000010001</mediaBlockid></mediaBlock>
 </predictixOfferMessage>
 <predictixOfferMessage>
   <Offer><offerid>1000003</offerid></Offer>
   <mediaBlock><mediaBlockid>90000000010003</mediaBlockid></mediaBlock>
 </predictixOfferMessage>
</Root>'));

Call the XSLT_SHRED_BATCH stored procedure with the stylesheet mapping to shred the XML data stored in the staging input table. The following arguments are used for the call.

  • The queryString is: sel * from xsltuser.Input_Docs
  • The xsltMapping argument is supplied by invoking the CREATEXML function with a stylesheet as input. This stylesheet will be applied to the XML document.
  • The externalContext is NULL for this example.
  • The resultCode is returned in :res. A successful operation returns 0.
CALL TD_SYSXML.XSLT_SHRED_BATCH('sel * from xsltuser.Input_Docs', CREATEXML('<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/Root">
 <Transaction>
  <Insert>
   <Table>
    <xsltuser.Offer>
	    <xsl:for-each select="predictixOfferMessage">
	     <Row>
	      <xsl:copy-of select="Offer/offerid"/>
	      <mediaBlockid><xsl:value-of select="mediaBlock/mediaBlockid"/></mediaBlockid>
	     </Row>
	    </xsl:for-each>
	   </xsltuser.Offer>
   </Table>
  </Insert>
 </Transaction>
</xsl:template>
</xsl:stylesheet>'), NULL, :res);
Result: To view the updated data in the target table, run: SELECT offerid, mediaBlockid FROM Offer;
offerid     mediaBlockid
----------  ----------------------------
1000003     90000000010003
1000001     90000000010001

This is another example on how to use both the <xsl:copy-of select=""> and <xsl:value-of select=""> mapping definitions.

Before running the example, ensure the test user is created with the required permissions and the required tables are created. See Setting Up the XSLT_SHRED_BATCH and XSLT_SHRED Examples for details.

Populate the staging table used to store the XML source document.

DELETE Customer;
DELETE Input_Docs;

INSERT INTO Input_Docs VALUES(1,
 CREATEXML('<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
  <c_custkey>1805</c_custkey>
  <c_name>Customer#000001805</c_name>
  <c_address>ZERs4Cu5lQTYD</c_address>
  <c_nationkey>9</c_nationkey>
  <c_phone>19-679-706-1096</c_phone>
  <c_acctbal>-274.75</c_acctbal>
  <c_mktsegment>AUTOMOBILE</c_mktsegment>
  <c_comment>quickly unusual courts alongside of the requests</c_comment>
</customer>
<customer>
  <c_custkey>1806</c_custkey>
  <c_name>Customer#000001806</c_name>
  <c_address>BB6Vr7W rSIpWKp</c_address>
  <c_nationkey>9</c_nationkey>
  <c_phone>19-872-322-3433</c_phone>
  <c_acctbal>254.17</c_acctbal>
  <c_mktsegment>MACHINERY</c_mktsegment>
  <c_comment>ideas are blithely. ironic instructions wake quickly.</c_comment>
</customer>
</customers>'));

Call the XSLT_SHRED_BATCH stored procedure with the stylesheet mapping to shred the XML data stored in the staging input table. The following arguments are used for the call.

  • The queryString is: sel id, xmldoc from xsltuser.Input_Docs
  • The xsltMapping argument is supplied by invoking the CREATEXML function with a stylesheet as input. This stylesheet will be applied to the XML document.
  • The externalContext is NULL for this example.
  • The resultCode is returned in :res. A successful operation returns 0.
CALL TD_SYSXML.XSLT_SHRED_BATCH('SEL id, xmldoc FROM xsltuser.Input_Docs', 
CREATEXML('<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/customers">
 <Transaction>
  <Insert>
   <Table>
    <xsltuser.customer>
     <xsl:for-each select="customer">
      <Row>
       <c_custkey genexp="cast(? as integer)">
         <xsl:value-of select="c_custkey"/>
       </c_custkey>
       <xsl:copy-of select="c_name"/>
       <xsl:copy-of select="c_address"/>
       <c_nationkey genexp="cast(? as integer)">
         <xsl:value-of select="c_nationkey"/>
       </c_nationkey>
       <xsl:copy-of select="c_phone"/>
       <c_acctbal genexp="cast(? as numeric(15,2))">
         <xsl:value-of select="c_acctbal"/>
       </c_acctbal>
       <xsl:copy-of select="c_mktsegment"/>
       <xsl:copy-of select="c_comment"/>
      </Row>
     </xsl:for-each>
    </xsltuser.customer>
   </Table>
  </Insert>
 </Transaction>
</xsl:template>
</xsl:stylesheet>'), NULL,:res);

Result: To view the updated data in the target table, run: SELECT * FROM customer;

c_custkey c_name              c_address      c_nationkey c_phone            c_acctbal c_mktsegment c_comment
--------- ------------------- --------------- ----------- --------------- ------------ ------------ ----------------------
     1805 Customer#000001805  ZERs4Cu5lQTYD             9 19-679-706-1096      -274.75 AUTOMOBILE   quickly unusual courts 
     1806 Customer#000001806  BB6Vr7W rSIpWKp           9 19-872-322-3433       254.17 MACHINERY    ideas are blithely