我已经创建了一个样式表,该样式表应该有选择地复制XML文档的内容,以便我可以去掉不需要的数据。我在下面提供了2个示例和我们当前使用的样式表来执行此操作。样式表工作正常,但我认为可能有更好的方法,因为在当前版本中,我在两个不同的位置检查相同的东西(作者='John Doe')。更高性能的XSLT - 输出中的选择性包含
针对包括在输出XML元素的规则如下:
- 如果存在具有提交文本等于“John Doe的”记事本中的记事本元件然后包括在该记事本元件输出
- 如果记事本元素的作者元素的文本等于'John Doe',则将所有元素包含在xml输出中的记事本元素中。
输入示例#1
<transaction>
<policy>
<insco>CC</insco>
<notepads>
<notepad>
<author>Andy</author>
<notepad>
<notepad>
<author>John Doe</author>
<notepad>
<notepad>
<author>Barney</author>
<notepad>
</notepads>
</policy>
</transaction>
用于输入#预期成果1
<transaction>
<policy>
<insco>CC</insco>
<notepads>
<notepad>
<author>John Doe</author>
<notepad>
</notepads>
</policy>
</transaction>
输入示例#2
<transaction>
<policy>
<insco>CC</insco>
<notepads>
<notepad>
<author>Andy</author>
<notepad>
</notepads>
</policy>
</transaction>
用于输入#预期结果2
<transaction>
<policy>
<insco>CC</insco>
</policy>
</transaction>
样式表的当前版本
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn">
<xsl:template match="*">
<xsl:choose>
<xsl:when test="name()='notepads'">
<xsl:if test="/transaction/policy/insco='CC' and (notepad/author='John Doe')">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:if>
</xsl:when>
<xsl:when test="name()='notepad'">
<xsl:if test="author='John Doe'">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
谢谢你的回答。我不知道增加的复杂性是否值得(这个名字不应该改变,或者如果它改变了,它会每两年改变一次),但我可以肯定地看到你在这里展示的技术的实用性。 – jwmajors81
这两种方法的复杂性大致相当 - 当然取决于您对XSLT的熟悉程度。无论如何,你决定。 ;) – Tomalak
在我看来,你“失去了”/ transaction/policy/insco ='CC''检查... – Lucero