开发者

How to display attributes depending upon the attribute value using XSLT?

开发者 https://www.devze.com 2023-01-04 09:04 出处:网络
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 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" s开发者_如何学Chould be returned.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消