XSLT: extract the last x digit of a sibling node with xpath expression

siosios

New Member
I am trying to extract the last 4 numbers of the "red" sibling with xpath.The source xml looks like:\[code\]... <node2> <key><![CDATA[RED]]></key> <value><![CDATA[98472978241908]]></value> ... more key value pairs here... </node2>...\[/code\]And when I use the follwing xpath: \[code\]/nodelevelX/nodelevelY/node2/key[text()='RED']/following-sibling::value\[/code\]I have the full number in output, then I tried to extract the digit with this xpath experssion:\[code\]/nodelevelX/nodelevelY/node2/key[text()='RED']/following-sibling::value/text()[substring(., string-length(.)-4)]\[/code\]I still have the full number. The substring function does not seems to work. my xsl header is: \[code\]<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">\[/code\]I think there is a small error, but I cannot see where. I followed many discussions on SO and others (w3schools) and tried to follow the advices whithout success.UPDATE: The context:I use the following identity which copy all the nodes from my source XML to the destination (xml) and I apply specific rules for some node after inside a xsl:template: \[code\]<!-- This copy the whole source XML in destination --> <xsl:template match="node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*" /> </xsl:copy> </xsl:template> <!-- specific rules for some nodes --> <xsl:template match="/nodeDetails"> <mynewnode> <!-- here I take the whole value and it s working --> <someVal><xsl:value-of select="/nodeDetails/nodeX/key[text()='ANOTHER_KEY']/following-sibling::value" /></someVal> <!-- FIXME substring does not work now --> <redVal><xsl:value-of select="/nodeDetails/nodeX/key[text()='RED']/following-sibling::value/text()[substring(.,string-length(.)-4)]" /></redVal> </mynewnode> </xsl:template>\[/code\]And for the transformation I use the following code from a junit class in Java (JDK 6):\[code\]@Testpublic void transformXml() throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Source xslt = new StreamSource(getClass().getResourceAsStream("contract.xsl")); Transformer transformer = factory.newTransformer(xslt); Source input = new StreamSource(getClass().getResourceAsStream("source.xml")); Writer output = new StringWriter(); transformer.transform(input, new StreamResult(output)); System.out.println("output=" + output.toString());}\[/code\]
 
Back
Top