Xml XSLT使用文本获取所有节点
我正试图从某个位置循环遍历文档中包含文本的所有节点Xml XSLT使用文本获取所有节点,xml,xslt,Xml,Xslt,我正试图从某个位置循环遍历文档中包含文本的所有节点 <xsl:template name="interpret_text"> <xsl:param name="location"/> <xsl:for-each select="$location//text()"> <xsl:choose> <xsl:when test="name(.) = tag_im_looking_for"> <
<xsl:template name="interpret_text">
<xsl:param name="location"/>
<xsl:for-each select="$location//text()">
<xsl:choose>
<xsl:when test="name(.) = tag_im_looking_for">
<!-- various code stuff and closing tags -->
底部将按此顺序运行
b context
c context
但是c上下文前后都有文本,我需要解析“before”,然后是“inner text”,然后是“after”。注意,我需要这个算法来处理任何深度的问题,无论是否有“before”或“after”文本。从节点解决方案或文本解决方案中是否有简单易行的方法来获得我想要的结果?我不清楚位置是什么,但此示例:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<sample> <xsl:call-template name="interpret_text">
<xsl:with-param name="location" select="."/>
</xsl:call-template>
</sample>
</xsl:template>
<xsl:template name="interpret_text">
<xsl:param name="location"/>
<xsl:for-each select="$location//text()">
<xsl:if test="string-length(normalize-space(.))>0">
<elem>
Parent: <xsl:value-of select="name(parent::*)"/>, Text: <xsl:value-of select="normalize-space(.)"/>
</elem>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
父项:,文本:
将通过您的输入样本生成此输出:
<sample>
<elem>
Parent: b, Text: before</elem>
<elem>
Parent: c, Text: inner text</elem>
<elem>
Parent: b, Text: after</elem>
</sample
父:b,文本:before
父级:c,文本:内部文本
父:b,文本:后
我不得不猜测一下你的实际需求,但这应该很接近
样式表:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<output>
<xsl:apply-templates match="foo"/>
</output>
</xsl:template>
<xsl:template name="interpret-text">
<xsl:for-each select=".//interesting-tag[text()]">
<xsl:value-of select="text()"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
文件:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xslt"?>
<root>
<foo>
<bar>
<interesting-tag>
Hello
</interesting-tag>
<interesting-tag>
World
</interesting-tag>
</bar>
<interesting-tag>
o11c was here
</interesting-tag>
</foo>
</root>
你好
世界
o11c在这里
也许你想要current()
?你知道node[cond]
xpath语法吗?我也不认为在调用时使用xsl:param
而不只是使用当前节点有什么意义……父节点:*就是我要找的操作符!谢谢
<sample>
<elem>
Parent: b, Text: before</elem>
<elem>
Parent: c, Text: inner text</elem>
<elem>
Parent: b, Text: after</elem>
</sample
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<output>
<xsl:apply-templates match="foo"/>
</output>
</xsl:template>
<xsl:template name="interpret-text">
<xsl:for-each select=".//interesting-tag[text()]">
<xsl:value-of select="text()"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xslt"?>
<root>
<foo>
<bar>
<interesting-tag>
Hello
</interesting-tag>
<interesting-tag>
World
</interesting-tag>
</bar>
<interesting-tag>
o11c was here
</interesting-tag>
</foo>
</root>