开发者

Read XML value based on attribute

开发者 https://www.devze.com 2023-02-07 20:17 出处:网络
I have a XML file as below : <?xml version=\"1.0\" encoding=\"utf-8\" ?> <?xml-stylesheet type=\"text/xsl\" href=\"XSLFILE.xslt\"?>

I have a XML file as below :

<?xml version="1.0" encoding="utf-8" ?> 
<?xml-stylesheet type="text/xsl" href="XSLFILE.xslt"?>
<books>
  <book ISBN="0321173481" author="Michael R. Sweet" >Book1</book>
  <book ISBN="0849371643" a开发者_StackOverflow社区uthor="Gerald Farin" >Book2</book>
  <book ISBN="A558606696" author="David Rogers" >Book3</book>
  <book ISBN="1568810849" author="Gerald Farin" >Book4</book>
</books>

I want to get the name of book (book1/book2/book3/book4) based on attribute value (ISBN or author) using XSLT.

Suppose if I write ISBN = 0321173481 then I should get value : book1.

Can anybody help me?

Thanks


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:param name="pISBN" select="0321173481"/>
 <xsl:param name="pAuthor" select="'David Rogers'"/>

 <xsl:template match="/">
  <xsl:value-of select="/*/*[@ISBN=$pISBN]"/>
  =====
  <xsl:value-of select="/*/*[@author=$pAuthor]"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the corrected version of your severely malformed pseudo-XML:

<books>
    <book ISBN="0321173481" author="Michael R. Sweet"
    >Book1</book>
    <book ISBN="0849371643" author="Gerald Farin"
    >Book2</book>
    <book ISBN="A558606696" author="David Rogers"
    >Book3</book>
    <book ISBN="1568810849" author="Gerald Farin"
    >Book4</book>
</books>

produces the two wanted and correct results:

  Book1
  =====
  Book3

Do note that this isn't an XSLT question, but an XPath question.


You can locate the name of a book with a given ISBN as follows:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:s="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"
    exclude-result-prefixes="msxsl">
  <xsl:output method="text"/>

  <!-- find a book with a certain isbn -->
  <xsl:template match="book[@ISBN=0321173481]">
    <!-- output the name -->
    <xsl:value-of select="."/>
  </xsl:template>

  <!-- visit all the documen nodes -->
  <xsl:template match="@*|node()">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:template>

</xsl:stylesheet>

This will output a text file with just the name of the selected book. You can of course make the ISBN value of '0321173481', the syntax for passing this in depends on your language.

However, if all you want to do is select the name of a book with a given ISBN, XSLT is not really the best technology, XSLT is designed for transformation of XML documents, not for simple queries.

FOr example you can find the book with a given ISBN using Linq to XML as follows:

var name = document.Descendants("book")
                   .Where(book => book.Attribute("ISBN").Value == "2")
                   .Single().Value;

Much simpler!

0

精彩评论

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