How to make a node result be different depending on the existence of a certain node using XLST?
My system produces a XML that contain a node that, depending on the type of event, came with different name.
开发者_高级运维The name can be <floatRate>
or <fixedRate>
. The path is always the same, only the node name that is different.
I need a transformation that can populate one field based on that name. The field will be called <type>
and the contend must be float or fixed, based on the node name.
Can this be done?
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="fixedRate|floatRate">
<type><xsl:value-of select=
"substring-before(name(),'Rate')"/></type>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<rates>
<fixedRate>2.3</fixedRate>
<floatRate>1.1</floatRate>
</rates>
produces the wanted result:
<rates>
<type>fixed</type>
<type>float</type>
</rates>
Select/mach expressions can do boolean "or", as in
match="floatRate|fixedRate"
Edit your question and add samples of input, expected output and what you've already tried to get more help
Suppose we have this document as input:
<result>
<floatRate>1.1</floatRate>
</result>
This stylesheet:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="result">
<type>
<xsl:value-of select="substring-before(name(floatRate|fixedRate),'Rate')"/>
</type>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Output:
<type>float</type>
精彩评论