作为XSLT世界的新手,我想基于属性值过滤XML文档,然后检索包含这些属性的元素并丢弃所有其他属性。
示例XML文档
<Items>
<item1>
<first>Lorem ipsum</first>
<second att="abc"> dolor sit amet</title>
<third>consectetur adipiscing elit, sed do eiusmod tempor</third>
<forth>ncididunt ut labore et dolore</forth>
</item1>
<item2>
<first att="def">Sed ut perspiciatis</first>
<second>unde omnis iste natus.</title>
<third att="ghi">error sit voluptatem accusantium</third>
<forth>doloremque laudantium</forth>
</item2>
<item3>
<first att="mno">At vero eos et </first>
<second>accusamus et iust</title>
<third>odio dignissimos ducimus qui blanditiis p</third>
<forth att="jkl">voluptatum deleniti atque</forth>
</item3>
<item4>
<first>Et harum quidem</first>
<second att="abc">rerum </title>
<third att="xyz"> soluta nobis est eligendi </third>
<forth> Temporibus aut</forth>
</item4>
我希望得到这个结果(需要att =“abc”):
<Items>
<item1>
<first>Lorem ipsum</first>
<second att="abc"> dolor sit amet</title>
<third>consectetur adipiscing elit, sed do eiusmod tempor</third>
<forth>ncididunt ut labore et dolore</forth>
</item1>
<item4>
<first>Et harum quidem</first>
<second att="abc">rerum </title>
<third att="xyz"> soluta nobis est eligendi </third>
<forth> Temporibus aut</forth>
</item4>
</items>
我找到了类似的解决方案here,但它对我不起作用。另外,我认为我应该使用for-each表达式,但我不知道该怎么做。
答案 0 :(得分:1)
尝试:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Items">
<Items>
<xsl:copy-of select="*[*/@att='abc']"/>
</Items>
</xsl:template>
</xsl:stylesheet>