And*_*ndy 2 xslt select apply-templates
为了得到答案,我看起来高低不一,并尝试了数百种排列,但一切都没有效果.
我正在尝试处理第一个节点的简单XML文档EXCEPT的所有<title>节点.基本上,我正在尝试找到一个xslt指令来完成它的精确反转
<xsl:apply-templates select="/topic/title"/>
Run Code Online (Sandbox Code Playgroud)
这是我的源XML:
<topic>
<title>Name of the Document</title>
<p>Document body</p>
<topic>
<title>First Document Subtopic</title>
<p>Body text for first document subtopic</p>
</topic>
<p>Body text continued for document</p>
<topic>
<title>Second Document Subtopic</title>
<p>Body text for document subtopic</p>
<topic>
<title>First Document Sub-subtopic</title>
<p>Body text for first document sub-subtopic</p>
</topic>
</topic>
<p>Body text continued a second time for document</p>
</topic>
Run Code Online (Sandbox Code Playgroud)
这是我的XSLT(我最初认为在应用模板的调用中应该起作用):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output omit-xml-declaration="yes" encoding="UTF-8" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<BOX>
<TEXTFLOW>
<xsl:apply-templates select="*[not(/topic/title)]"/>
</TEXTFLOW>
</BOX>
</xsl:template>
<xsl:template match="topic">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="title">
<PARA STYLE="Title"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="p">
<PARA STYLE="BodyText"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()">
<FORMAT>
<xsl:value-of select="."/>
</FORMAT>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
这就是我想看到的(注意第一<title>件事的缺席):
<BOX>
<TEXTFLOW>
<PARA STYLE="BodyText"/>
<FORMAT>Document body</FORMAT>
<PARA STYLE="Title"/>
<FORMAT>First Document Subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text for first document subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text continued for document</FORMAT>
<PARA STYLE="Title"/>
<FORMAT>Second Document Subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text for document subtopic</FORMAT>
<PARA STYLE="Title"/>
<FORMAT>First Document Sub-subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text for first document sub-subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text continued a second time for document</FORMAT>
</TEXTFLOW>
</BOX>
Run Code Online (Sandbox Code Playgroud)
我可以使用什么选择表达式来完成此任务?
就这么简单:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<BOX>
<TEXTFLOW>
<xsl:apply-templates/>
</TEXTFLOW>
</BOX>
</xsl:template>
<xsl:template match="/topic/title"/>
<xsl:template match="title">
<PARA STYLE="Title"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="p">
<PARA STYLE="BodyText"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()">
<FORMAT>
<xsl:value-of select="."/>
</FORMAT>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
在提供的XML文档上应用此转换时:
<topic>
<title>Name of the Document</title>
<p>Document body</p>
<topic>
<title>First Document Subtopic</title>
<p>Body text for first document subtopic</p>
</topic>
<p>Body text continued for document</p>
<topic>
<title>Second Document Subtopic</title>
<p>Body text for document subtopic</p>
<topic>
<title>First Document Sub-subtopic</title>
<p>Body text for first document sub-subtopic</p>
</topic>
</topic>
<p>Body text continued a second time for document</p>
</topic>
Run Code Online (Sandbox Code Playgroud)
产生了想要的正确结果:
<BOX>
<TEXTFLOW>
<PARA STYLE="BodyText"/>
<FORMAT>Document body</FORMAT>
<PARA STYLE="Title"/>
<FORMAT>First Document Subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text for first document subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text continued for document</FORMAT>
<PARA STYLE="Title"/>
<FORMAT>Second Document Subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text for document subtopic</FORMAT>
<PARA STYLE="Title"/>
<FORMAT>First Document Sub-subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text for first document sub-subtopic</FORMAT>
<PARA STYLE="BodyText"/>
<FORMAT>Body text continued a second time for document</FORMAT>
</TEXTFLOW>
</BOX>
Run Code Online (Sandbox Code Playgroud)
说明:
要从处理中排除所需元素,我们使用匹配它们且没有正文的模板:
<xsl:template match="/topic/title"/>
Run Code Online (Sandbox Code Playgroud)
II.更新
不推荐以下替代解决方案,因为它是"拉式"的示例,并且不像上面的"推式"解决方案那样灵活,优雅和可维护,但是OP坚持认为他需要这样的劣质解决方案:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<BOX>
<TEXTFLOW>
<xsl:apply-templates
select="*/*[not(self::title)]"/>
</TEXTFLOW>
</BOX>
</xsl:template>
<xsl:template match="title">
<PARA STYLE="Title"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="p">
<PARA STYLE="BodyText"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()">
<FORMAT>
<xsl:value-of select="."/>
</FORMAT>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当在提供的XML文档(上面)上应用此转换时,它会再次生成所需的正确结果.