XSLT - 检索没有其内部标签的 XMl 标签值 [英] XSLT - retrieving an XMl tag value without its inner tags
本文介绍了XSLT - 检索没有其内部标签的 XMl 标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的 xml:
I have an xml that looks like this:
<OuterTag> outerVal
<Name> value1 </Name>
<Desc> value2 </Desc>
</OuterTag>
我想检索外部标签的值(outerVal").当我使用
and I want to retrieve the value of the outer tag ("outerVal"). when I use
xsl:value-of select="OuterTag" />
我得到outerValvalue1value2".如何只检索外部值?
I get "outerValvalue1value2". How can i retrieve only the outer value?
推荐答案
一个完整的解决方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" method="text"/>
<xsl:template match="/">
<xsl:value-of select="normalize-space(OuterTag/text()[1])" />
</xsl:template>
</xsl:stylesheet>
输出:
outerVal
注意: 空格在 XML 元素中很重要.这是一个样式表,它显示了 OuterTag
及其子节点中文本节点的位置/结构:
Note: Whitespace is significant inside XML elements. Here's a stylesheet that reveals the placement/structure of the text nodes in OuterTag
and its children:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="text" />
<xsl:template match="OuterTag/text()">
<xsl:value-of select="concat('[', ., ']')" />
</xsl:template>
<xsl:template match="OuterTag/*/text()">
<xsl:value-of select="concat('(', ., ')')" />
</xsl:template>
</xsl:stylesheet>
输出:
[ outerVal
]( value1 )[
]( value2 )[
]
添加normalize-space
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="text" />
<xsl:template match="OuterTag/text()">
<xsl:value-of select="concat('[', normalize-space(), ']')" />
</xsl:template>
<xsl:template match="OuterTag/*/text()">
<xsl:value-of select="concat('(', normalize-space(), ')')" />
</xsl:template>
</xsl:stylesheet>
产生以下结果:
[outerVal](value1)[](value2)[]
这篇关于XSLT - 检索没有其内部标签的 XMl 标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文