Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select node with unique id using XSLT

Tags:

xml

xslt

How can I select a specific node with an unique ID and return the entire node as xml.

<xml>
<library>
<book id='1'>
<title>firstTitle</title>
<author>firstAuthor</author>
</book>
<book id='2'>
<title>secondTitle</title>
<author>secondAuthor</author>
</book>
<book id='3'>
<title>thirdTitle</title>
<author>thirdAuthor</author>
</book>
</library>
</xml>

In this case I would like to return book with id='3', so it will look something like this:

<book id='3'>
<title>thirdTitle</title>
<author>thirdAuthor</author>
</book>
like image 201
Newton Avatar asked Jun 27 '12 05:06

Newton


People also ask

Which function can be used to generate a unique ID in XSLT?

XSLT generate-id() Function The generate-id() function returns a string value that uniquely identifies a specified node. If the node-set specified is empty, an empty string is returned. If you omit the node-set parameter, it defaults to the current node.

How do I select a substring in XSLT?

XSLT doesn't have any new function to search Strings in a reverse manner. We have substring function which creates two fields substring-before-last and substring-after-last.In XSLT it is defined as follows: <xsl:value-of select="substring (string name ,0, MAX_LENGTH )"/>...

What is Number () in XSLT?

Definition and Usage. The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.


1 Answers

If you are referring to XPath (because you are searching in the document, not transforming it), that would be:

//book[@id=3]

Of course, depending on your language, there might be a library that makes this search even simpler.

like image 145
Kobi Avatar answered Nov 08 '22 15:11

Kobi