Discussions
Categories
- 17.9K All Categories
- 3.4K Industry Applications
- 3.3K Intelligent Advisor
- 62 Insurance
- 536K On-Premises Infrastructure
- 138.2K Analytics Software
- 38.6K Application Development Software
- 5.7K Cloud Platform
- 109.4K Database Software
- 17.5K Enterprise Manager
- 8.8K Hardware
- 71.1K Infrastructure Software
- 105.2K Integration
- 41.5K Security Software
Value of attribute in xslt

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"/> </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
-
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
-
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"/> </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>
-
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"/> </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
-
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.
-
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.