Select node with unique id using XSLT

xml, xslt

Solution

This XSLT 1.0 style-sheet ...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
  <xsl:apply-templates select="*/*/book[@id='3']" />
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
   <xsl:apply-templates select="@*|node()" />
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

... will transform your sample input document to the stated sample output document

Problem

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> ```

Original source