Forum Stats

  • 3,824,767 Users
  • 2,260,416 Discussions
  • 7,896,309 Comments

Discussions

Value of attribute in xslt

user8195117
user8195117 Member Posts: 56 Red Ribbon
edited Aug 12, 2020 1:41PM in XML DB

Hello,

I have a XML node with attributes as below

<PD1_PROD_ID1 Error_Flag="Y" Error_Msg="Product Identification data is missing in PD1_PROD_ID1">Product Identification data is missing in PD1_PROD_ID1</PD1_PROD_ID1>

I need to get the value of Error_Msg printed when PD1_PROD_ID1/@Error_Flag=''Y'' . I am using the below XSLT.....but it is not printing the attribute value of Error_Msg

<xsl:if test="PD1_PROD_ID1">

                <xsl:choose> <xsl:when test ="PD1_PROD_ID1/@Error_Flag=''Y''">

                <font color="red"><xsl:value-of select="PD1_PROD_ID1/@Error_Msg"/&gt; </font> <xsl:text>*</xsl:text></xsl:when>               

                <xsl:otherwise>

                <xsl:value-of select="PD1_PROD_ID1"/><xsl:text>*</xsl:text>

                </xsl:otherwise>  </xsl:choose>

                </xsl:if>

Its Oracle 11g R2. Please help

Best Answer

  • user8195117
    user8195117 Member Posts: 56 Red Ribbon
    edited Aug 12, 2020 1:41PM Answer ✓

    Hi Jason,

    My bad... I was using the default XML where all the Error_flags were N and hence it was going to Otherwise. I modified the code to pick the correct XML and the original xslt posted in the question worked as expected.

Answers

  • Jason_(A_Non)
    Jason_(A_Non) Member Posts: 2,094 Silver Trophy
    edited Aug 11, 2020 3:34PM

    Having no clue at all how you are using this snippet, all I can say is the following works on 11.2.0.4

    declare    l_xml    XMLTYPE;   l_xslt   XMLTYPE;   l_result CLOB;begin   l_xml := XMLTYPE('<?xml version="1.0" encoding="UTF-8"?><root>   <PD1_PROD_ID1 Error_Flag="Y" Error_Msg="Product Identification data is missing in PD1_PROD_ID1">Product Identification data is missing in PD1_PROD_ID1</PD1_PROD_ID1></root>');   l_xslt := XMLTYPE('<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">   <xsl:template match="/">      <xsl:apply-templates select="root"/>   </xsl:template>   <xsl:template match="root">     <container>      <xsl:if test="PD1_PROD_ID1">         <xsl:choose>            <xsl:when test="PD1_PROD_ID1/@Error_Flag=''Y''">               <font color="red">                  <xsl:value-of select="PD1_PROD_ID1/@Error_Msg"/&gt;               </font>            </xsl:when>            <xsl:otherwise>               <xsl:value-of select="PD1_PROD_ID1"/>            </xsl:otherwise>         </xsl:choose>         <xsl:text>*</xsl:text>      </xsl:if>      </container>   </xsl:template></xsl:stylesheet>');   l_result := 'a';   SELECT xmltransform(l_xml, l_xslt).getClobVal()     INTO l_result     FROM dual;   dbms_output.put_line(l_result);   dbms_output.new_line;   dbms_output.put_line(l_xml.transform(l_xslt).getclobval());end;

    produces

    <container xmlns:fo="http://www.w3.org/1999/XSL/Format"> <font color="red">Product Identification data is missing in PD1_PROD_ID1</font>*</container><container xmlns:fo="http://www.w3.org/1999/XSL/Format">  <font color="red">Product Identification data is missing in PD1_PROD_ID1</font>*</container>
  • Jason_(A_Non)
    Jason_(A_Non) Member Posts: 2,094 Silver Trophy
    edited Aug 12, 2020 10:20AM

    FYI, most of the XML and XSLT you posted was not needed.  Here is your data trimmed down to the bare minimum in regards to the issue you reported

    declare      l_xml    XMLTYPE;     l_xslt   XMLTYPE;     l_result CLOB;  begin     l_xml := XMLTYPE('<?xml version="1.0"?><ROWSET>   <ROW>      <XML_DATA>         <PH1>            <PD1>               <PD1_PROD_ID1 Error_Flag="N" Error_Msg="N">XXX</PD1_PROD_ID1>            </PD1>            <PD1>               <PD1_PROD_ID1 Error_Flag="N" Error_Msg="N">XXX</PD1_PROD_ID1>            </PD1>            <PD1>               <PD1_PROD_ID1 Error_Flag="N" Error_Msg="N">XXX</PD1_PROD_ID1>            </PD1>            <PD1>               <PD1_PROD_ID1 Error_Flag="Y" Error_Msg="Product Identification data is missing in PD1_PROD_ID1">Product Identification data is missing in PD1_PROD_ID1</PD1_PROD_ID1>            </PD1>         </PH1>      </XML_DATA>   </ROW></ROWSET>');     l_xslt := XMLTYPE('<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:output method="html"/>   <xsl:template match="/">      <xsl:for-each select="/ROWSET/ROW/XML_DATA/PH1/PD1">         <xsl:if test="PD1_PROD_ID1">            <xsl:choose>               <xsl:when test="PD1_PROD_ID1/@Error_Flag=''Y''">                  <font color="red">                     <xsl:value-of select="PD1_PROD_ID1/@Error_Msg"/&gt;                  </font>                  <xsl:text>*</xsl:text>               </xsl:when>               <xsl:otherwise>                  <xsl:value-of select="PD1_PROD_ID1"/>                  <xsl:text>*</xsl:text>               </xsl:otherwise>            </xsl:choose>         </xsl:if>      </xsl:for-each>   </xsl:template></xsl:stylesheet>');   SELECT XMLTRANSFORM (l_xml, l_xslt).getclobval () INTO l_result FROM DUAL;      dbms_output.put_line(l_result);  end;

    and that outputs

    XXX*XXX*XXX*<font color="red">Product Identification data is missing in PD1_PROD_ID1</font>*

    and it looks right to me.

    Use the shell I started to add in complexity until you find a change that produces the incorrect output then we can use that to find the issue.  Again, the above was ran on 11.2.0.4

  • Jason_(A_Non)
    Jason_(A_Non) Member Posts: 2,094 Silver Trophy
    edited Aug 12, 2020 12:50PM

    Change the one line in your XML to look like

    <PD1_PROD_ID1 Error_Flag="Y" Error_Msg="This is attribute Error_Msg">Product Identification data is missing in PD1_PROD_ID1</PD1_PROD_ID1>

    and run it again.  You'll see the difference.

    The attribute value and contents of the node are exactly the same in your original message.  It is working right, you just can't tell the difference without changing the input XML to verify.

  • user8195117
    user8195117 Member Posts: 56 Red Ribbon
    edited Aug 12, 2020 1:41PM Answer ✓

    Hi Jason,

    My bad... I was using the default XML where all the Error_flags were N and hence it was going to Otherwise. I modified the code to pick the correct XML and the original xslt posted in the question worked as expected.