Arun
Arun

Reputation: 1482

Getting values from xml using xslt

I have a sql stored procedure which returns the xml data.the select command of my SP is

SELECT TXT1 
FROM @tmp1 CT
    LEFT JOIN ETEXT TT ON CT.Cate = TT.REFKEY FOR XML AUTO 

it returns the xml like

<root>
<item>
<XML_F52E2B61-18A1-11d1-B105-00805F49916B>
<TT TXT1="Mouse"/>
<TT TXT1="Computer"/>
</XML_F52E2B61-18A1-11d1-B105-00805F49916B>
</item>
</root>

I need to style this xml using xslt and print the TXT1 values, from last TXT1 value to First order.I tried some code but it is not possible to get my answer .my code like

<xsl:variable name="cate" select="shop:ExecStoredProcedure('kt_cate',concat('@Dcat:',$default))">
    <xsl:variable name="txt1-atts" select="$cate//TT/@TXT1"/>

Anybody help to get my answer?

Upvotes: 1

Views: 192

Answers (1)

ABach
ABach

Reputation: 3738

It's not especially clear what you're trying to accomplish - that said, if we take your comment above:

Yes I need to select all TXT1 values from the xml from last to first means Last value Computer print first ans Mouse Print next like that

...then this XSLT 1.0 solution accomplishes what you want.

When this XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes" method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/*">
    <xsl:apply-templates select="*/*/TT[@TXT1]">
      <xsl:sort select="position()" order="descending"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="TT">
    <xsl:value-of select="concat(@TXT1, '&#10;')"/>
  </xsl:template>

</xsl:stylesheet>

...is applied against the provided XML:

<root>
  <item>
    <XML_F52E2B61-18A1-11d1-B105-00805F49916B>
      <TT TXT1="Mouse"/>
      <TT TXT1="Computer"/>
    </XML_F52E2B61-18A1-11d1-B105-00805F49916B>
  </item>
</root>

...the wanted result is produced:

Computer
Mouse

Explanation:

The XSLT is made up of two templates:

  • The first template matches the first element in the document and, upon finding it, instructs the XSLT processor to apply templates to all great-grandchildren <TT> elements that have a @TXT1 attribute. Most importantly, the processor is instructed to apply templates to these elements in reverse order (via the <xsl:sort> instruction).
  • The second template matches all <TT> elements (which, given the solitary existence of the previous template, means that it will match all <TT> elements that have a @TXT1 attribute). Upon finding one such element, the value of its @TXT1 attribute is printed, followed by a newline.

Upvotes: 2

Related Questions