xml – 使用XSLT选择具有唯一ID的节点

前端之家收集整理的这篇文章主要介绍了xml – 使用XSLT选择具有唯一ID的节点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何选择具有唯一ID的特定节点并将整个节点作为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>

在这种情况下,我想返回id =’3’的书,所以它看起来像这样:

<book id='3'>
<title>thirdTitle</title>
<author>thirdAuthor</author>
</book>

解决方法

这个XSLT 1.0样式表……

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

…将您的样本输入文档转换为所述的样本输出文档

猜你在找的XML相关文章