tags:

views:

28

answers:

1

Hi All,
We are using XSLT to display xml attributes depending upon their value. We are able to do it from server side using C#. But we are not getting how to achieve it through XSLT.
We are using sample xml as;

<BookInfo>
   <BookTable show="Book 1" >
       <book id="book1" value="Book 1" />
       <book id="book2" value="Book 2" />
   </BookTable>
</BookInfo>


We want to read "show" attribute value and depending upon the value, we want to display the node information.
Please help me to achieve this using XSLT. Thanks in advance.

Modified xml;

<Book>
  <Info>
    <Item name="Item1" type="DropDown" defaultValue="Two" values="One,Two,Three" />
    <Item name="One" type="Label" value="some text"  />
    <Item name="Two" type="TextBox" value="another text"  />
    <Item key="Three" name="CheckBox"  value="true" />
  </Info>
</Book>

Unfortunately the xml format got changed. Now, in this case, for Item1 defaultValue is two, so node with name "two" should be returned.

+1  A: 

It's something like:

<xsl:template match="BookTable">
  <xsl:value-of select="book[@value = current()/@show]"/>
</xsl:template>

EDIT
Your second example isn't quite clear, I'm assuming there is always a <Item name="Item1"> node, that points to the real node that should be displayed.

<xsl:template match="Info">
  <xsl:copy-of select="Item[@name = Item[@name='Item1']/@defaultValue]" />
</xsl:template>

In the XPath you need a @ to get at the value of an attribute of your input-xml.
The Item[@name = ...] selects an item where the value of the name attribute equals the specified value.
The current() gives access to the current node that the template is processing. You need that as a plain @show would select the value of that attribute for the node being selected. Example: an Item[@name = @defaultValue] would select Items where the values for 'name' and 'defaultValue' are identical.

Hans Kesting
Hello Hans,Thanks for your reply. But, unfortunately the xml format got changed.
Vijay Balkawade
@Vijay, I extended my answer.
Hans Kesting
@Hans Kesting: You can say that `match="Item[@name=../Item/@defaultValue]"` is a general way to go if you could trust in this schema. See my comment about the question.
Alejandro