Jump to content

Retreiving Particular Element From Specific Tree


krooklyn

Recommended Posts

Hi, Relative newbie, so bare with me. I've done a bit of searching and came up with nothing that will work as of yet. If anyone has a suggestion, then please let me know. Thanks.Trying to grab the particular values from the respective value elements for only the tree that meet the condition as noted by the <xsl: when test=.../> bit. For example. For Value1a, I want to only grab the ValueE value from tree where ValueC='Value 1'. The problem I'm running into is that b/c ValueB is not a parent element within the tree, but a child of the same level as ValueC, when the xslt grabs the values for Value1a and Value1b just fine assuming there's no other trees with similar nodes. However, when it goes to Value2a and Value2b it just grabs the values from ValueE and ValueD from the first tree in the XML, where I want it grab it from the tree where ValueC='Value 2'as prefaced by the <xsl: when test=...> bit. I looked and couldn't find a way of specifying which tree to grab the elements from. I even got a little creative for the xslt for Value2a and Value2b, but it still grabbed the data from the first tree.XML snippet: <ValueA> <ValueB> <ValueD>USD</ValueD> <ValueE>32000</ValueE> </ValueB> <ValueC>Value 1</ValueC> </ValueA> <ValueA> <ValueB> <ValueD>USD</ValueD> <ValueE>22000</ValueE> </ValueB> <ValueC>Value 2</ValueC> </ValueA>XSLT snippet (1st variant): <Value1a> <xsl:text>"</xsl:text> <xsl:choose> <xsl:when test="//ValueA/ValueC[.=Value 1]"><xsl:value-of select="//ValueA/ValueB/ValueE"/></xsl:when> </xsl:choose> <xsl:text>"</xsl:text> </Value1a> <Value1b> <xsl:text>"</xsl:text> <xsl:choose> <xsl:when test="//ValueA/ValueC[.=Value 1]"><xsl:value-of select="//ValueA/ValueB/ValueD"/></xsl:when> </xsl:choose> <xsl:text>"</xsl:text> </Value1b> <Value2a> <xsl:text>"</xsl:text> <xsl:choose> <xsl:when test="//ValueA/ValueC[.=Value 2]"><xsl:value-of select="//ValueA/ValueC[.=Value 2]/ValueB/ValueE"/> </xsl:when> </xsl:choose> <xsl:text>"</xsl:text> </Value2a> <Value2b> <xsl:text>"</xsl:text> <xsl:choose> <xsl:when test="//ValueA/ValueC[.=Value 2]"><xsl:value-of select="//ValueA/ValueC[.=Value 2]/ValueB/ValueD"/></xsl:when> </xsl:choose> <xsl:text>"</xsl:text> </Value2b>

Link to comment
Share on other sites

Please post well-formed and indented XML input and result samples, then I am sure we can show you the XSLT code to achieve that.
Sorry Martin. The formatting was lost in translation it would seem. XML:
<ValueA>	<ValueB>		<ValueD>USD</ValueD>		<ValueE>32000</ValueE>	</ValueB>	<ValueC>Value 1</ValueC></ValueA><ValueA>	<ValueB>		<ValueD>USD</ValueD>		<ValueE>22000</ValueE>	</ValueB>	<ValueC>Value 2</ValueC></ValueA>XSLT:<Value1a>	<xsl:text>"</xsl:text>	<xsl:choose>		<xsl:when test="//ValueA/ValueC[.='Value 1']"><xsl:value-of select="//ValueA/ValueB/ValueE"/></xsl:when>	</xsl:choose>	<xsl:text>"</xsl:text></Value1a><Value1b>	<xsl:text>"</xsl:text>	<xsl:choose>		<xsl:when test="//ValueA/ValueC[.='Value 1']"><xsl:value-of select="//ValueA/ValueB/ValueD"/></xsl:when>	</xsl:choose>	<xsl:text>"</xsl:text></Value1b><Value2a>	<xsl:text>"</xsl:text>	<xsl:choose>		<xsl:when test="//ValueA/ValueC[.='Value 2']"><xsl:value-of select="//ValueA/ValueC[.='Value 2']/ValueB/ValueE"/>		</xsl:when>	</xsl:choose>	<xsl:text>"</xsl:text></Value2a><Value2b>	<xsl:text>"</xsl:text>	<xsl:choose>		<xsl:when test="//ValueA/ValueC[.='Value 2']"><xsl:value-of select="//ValueA/ValueC[.='Value 2']/ValueB/ValueD"/>		</xsl:when>	</xsl:choose>	<xsl:text>"</xsl:text></Value2b>

Sample Result:Value1a: 32000Value1b: USDValue2a: 32000Value2b: USDI would like to get '22000' for Value2a, but the XSLT is grabbing the first element from 'ValueE' in the first tree, where I'd like it to grab the element from the 'ValueE' tag within the second tree.

Link to comment
Share on other sites

You don't need that xsl:choose, you simply need e.g.
	<Value2a>		<xsl:text>"</xsl:text>		<xsl:value-of select="//ValueA[ValueC = 'Value 2']/ValueB/ValueE"/>		<xsl:text>"</xsl:text>	</Value2a>

Thanks Martin. Will try that and report back. :)
Link to comment
Share on other sites

Martin,That XSLT is failing validation, so I think for my purposes I need to keep the xsl:choose bit. :)
What exactly do you mean by "failing validation"? A well formedness error? What is the error message anyway?You still haven't showed your full XSLT and XML, so we can't really be sure if this is the problem. As far as I can see, Martin's sample is OK. It shouldn't cause any errors on its own.
Link to comment
Share on other sites

Martin,That XSLT is failing validation, so I think for my purposes I need to keep the xsl:choose bit. :)
That is a snippet of XSLT you need to put in a template which you need to put in a stylesheet:
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">    <xsl:output indent="yes"/>    <xsl:template match="/">	<Value2a>		<xsl:text>"</xsl:text>		<xsl:value-of select="//ValueA[ValueC = 'Value 2']/ValueB/ValueE"/>		<xsl:text>"</xsl:text>	</Value2a>  </xsl:template></xsl:stylesheet>

That is about all I can suggest. It shows how to get the correct result for the 'Value2A' element, you can use the same approach for the other elements. If you need more help then post a well-formed XML input document and the corresponding XML output document you want to create, so far you are only showing snippets or not even the result XML.Or if you want to solve that "validation" problem then you need to provide more details as to how you validate exactly and which error(s) exactly you get.

Link to comment
Share on other sites

What exactly do you mean by "failing validation"? A well formedness error? What is the error message anyway?You still haven't showed your full XSLT and XML, so we can't really be sure if this is the problem. As far as I can see, Martin's sample is OK. It shouldn't cause any errors on its own.
The stylesheet is used by a tool that's proprietary (hence why I cannot provide the full XML and XSLT), which is java based. The error I'm getting is an error from the tool."javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet"However, if I input the XSLT and XML via an onine transformer the value is present in the ouput. So, it would appear to be an issue with the tool. :) Looks like I'll have to speak with the developer of the tool. Thanks for your help guys. :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...