admin管理员组文章数量:1125752
I have below XML where I want to extract the p2 element value for p1 value is Orange using XSLT and then show that value in the variablePrint tag.
<rootyFruity xmlns:h="/">
<h:fruit>
<h:p1>Apple</h:p1>
<h:p2>1</h:p2>
</h:fruit>
<h:fruit>
<h:p1>Orange</h:p1>
<h:p2>2</h:p2>
</h:fruit>
<h:fruit>
<h:p1>Guava</h:p1>
<h:p2>3</h:p2>
</h:fruit>
<h:fruit>
<h:p1>Grapes</h:p1>
<h:p2>4</h:p2>
</h:fruit>
<h:variablePrint>ssss</h:variablePrint>
</rootyFruity>
Below is the XSLT, I was able to form where I am just grabbing the variablePrint element and displaying value 5 in it using a variable.
Is there a way to extract p2 element value when p1 value is Orange?
<xsl:stylesheet xmlns:xsl=";
xmlns:miscHelper=".webserver.util.MiscellaneousHelper"
version="1.0">
<xsl:variable name="variableName" select="5"/>
<xsl:output method="xml"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//*[local-name()='variablePrint']/text()">
<xsl:value-of select="$variableName"/>
</xsl:template>
</xsl:stylesheet>
I have below XML where I want to extract the p2 element value for p1 value is Orange using XSLT and then show that value in the variablePrint tag.
<rootyFruity xmlns:h="http://www.w3.org/TR/html4/">
<h:fruit>
<h:p1>Apple</h:p1>
<h:p2>1</h:p2>
</h:fruit>
<h:fruit>
<h:p1>Orange</h:p1>
<h:p2>2</h:p2>
</h:fruit>
<h:fruit>
<h:p1>Guava</h:p1>
<h:p2>3</h:p2>
</h:fruit>
<h:fruit>
<h:p1>Grapes</h:p1>
<h:p2>4</h:p2>
</h:fruit>
<h:variablePrint>ssss</h:variablePrint>
</rootyFruity>
Below is the XSLT, I was able to form where I am just grabbing the variablePrint element and displaying value 5 in it using a variable.
Is there a way to extract p2 element value when p1 value is Orange?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:miscHelper="http://www.jclark.com/xt/java/glog.webserver.util.MiscellaneousHelper"
version="1.0">
<xsl:variable name="variableName" select="5"/>
<xsl:output method="xml"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//*[local-name()='variablePrint']/text()">
<xsl:value-of select="$variableName"/>
</xsl:template>
</xsl:stylesheet>
Share
Improve this question
asked Jan 9 at 3:25
SandeepSandeep
8064 silver badges8 bronze badges
1 Answer
Reset to default 1You can set the variable this way:
<xsl:variable name="variableName" select="//h:fruit[h:p1='Orange']/h:p2"/>
That finds the matching fruit element and returns the content of its p2 child. You will need add the definition for the h: namespace prefix.
本文标签: xmlXSLT Code to extract an element value on basis of a conditionStack Overflow
版权声明:本文标题:xml - XSLT Code to extract an element value on basis of a condition - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736674464a1947105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论