Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2005, 04:40 PM
Registered User
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to get values depending on the attribute

i have an xml that looks like

 <contractions elementType="H">
    <description>(3s) -> [2s]</description>
    <contraction shell="S">
      <matrix rows="2" columns="2" dataType="double">
5.447178 0.156285
0.824547 0.904691
       </matrix>
    </contraction>
    <contraction shell="S">
      <matrix rows="1" columns="2" dataType="double">
0.183192 1.000000
       </matrix>
    </contraction>
 </contractions>

<contractions elementType="N">
    <description>(3s) -> [2s]</description>
    <contraction shell="S">
      <matrix rows="2" columns="2" dataType="double">
5.447178 0.156285
0.824547 0.904691
       </matrix>
    </contraction>
    <contraction shell="S">
      <matrix rows="1" columns="2" dataType="double">
0.183192 1.000000
       </matrix>
    </contraction>
 </contractions>


now i need to get the shell value (ie : S) and the matrix node values
ie(0.183192 1.000000 and 5.447178 0.156285 0.824547 0.904691 ).
I need to get the shell and matrix node values for the elementtype H only.can someone help me out with this

the xsl now looks like
    <xsl:template name="getElementShellType">
            <xsl:param name="Values"/>
        <xsl:if test="$Values">
        <xsl:for-each select="$Values">
             <xsl:value-ofselect="@elementType"></xsl:value-of>
                              <xsl:value-ofselect="@shell"></xsl:value-of>
         </xsl:for-each>
        </xsl:if>
            <xsl:text> </xsl:text>
        </xsl:template>

can someone help me out with this

 
Old July 8th, 2005, 03:55 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I don't like the XML, better to have each matrix value in a separate element. Given this XML:
Code:
<root>
  <contractions elementType="H">
    <description>(3s) -> [2s]</description>
    <contraction shell="S">
      <matrix rows="2" columns="2" dataType="double">
        5.447178 0.156285 
        0.824547 0.904691 
      </matrix>
    </contraction>
    <contraction shell="S">
      <matrix rows="1" columns="2" dataType="double">
        0.183192 1.000000 
    </matrix>
    </contraction>
  </contractions>
  <contractions elementType="N">
    <description>(3s) -> [2s]</description>
    <contraction shell="S">
      <matrix rows="2" columns="2" dataType="double">
5.447178 0.156285 
0.824547 0.904691 
       </matrix>
    </contraction>
    <contraction shell="S">
      <matrix rows="1" columns="2" dataType="double">
0.183192 1.000000 
       </matrix>
    </contraction>
  </contractions>
</root>
This stylesheet gives the values. I don't know how you're passing $Values, it must be a nodeset not a string to do a for-each.
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="/">
    <xsl:call-template name="getElementShellType">
      <xsl:with-param name="Values" select="'H'" />
    </xsl:call-template>
  </xsl:template>
  <xsl:template name="getElementShellType">
    <xsl:param name="Values" />
    <xsl:variable name="contractions" select="/*/contractions[@elementType = $Values]" />
    <xsl:for-each select="$contractions/contraction">
      <xsl:text>Shell:#xa0;</xsl:text>
      <xsl:value-of select="@shell" /><xsl:text>#x0d;</xsl:text>
      <xsl:text>Matrix:#xa0;</xsl:text>
      <xsl:value-of select="matrix" /><xsl:text>#x0d;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
getting distinct values from attribute markus2000 XSLT 1 June 13th, 2006 03:06 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
to get Distinct Node depending on attribute value chaitanyaXML XML 5 March 29th, 2005 10:26 AM
Button acts depending on radio button values janise Access 4 March 10th, 2004 12:53 AM
different values with the same attribute name srini XSLT 0 January 21st, 2004 05:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.