Friday, 15 January 2010

Changing the previous NODE value based on current node value in xslt -



Changing the previous NODE value based on current node value in xslt -

i struggling below issue. hope 1 of suggest or guide me resolve .

i have payload of below pseudo xsd type.

<element name=main maxcoccurs=unbounded> <complextype> <element name=input1/> <element name=input2/> <element name=input3/> <element name=input4/> <element name=input5/> <element name=inside/> <element name=username/> <element name=address/> </complextype> <element>

main repeatable element. want check username, if value in username of nodes not equal, need assign blank value first node username.

how can in transformation?

i trying below: firt username in variable, , check usernames using for-each. if not equal.... cant assign blank value first node, in username of nth node.

i thinking of using variable unmatched=true(), xslt cant allow create alter variable 1 time declared. alternative ruled out.

how can accomplish this?? help me resolve this..

hope clear.

cheers chandru

assuming foo name of parent element of main elements use

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* , node()"/> </xsl:copy> </xsl:template> <xsl:template match="foo[some $m in main satisfies $m/username != main/username]/main[1]/username"> <xsl:copy/> </xsl:template> </xsl:stylesheet>

here input sample:

<root> <foo> <main> <input1/> <input2/> <input3/> <input4/> <input5/> <inside/> <username>u1</username> <address/> </main> <main> <input1/> <input2/> <input3/> <input4/> <input5/> <inside/> <username>u1</username> <address/> </main> <main> <input1/> <input2/> <input3/> <input4/> <input5/> <inside/> <username>u2</username> <address/> </main> <main> <input1/> <input2/> <input3/> <input4/> <input5/> <inside/> <username>u1</username> <address/> </main> </foo> </root>

when stylesheet applied saxon 9.4, output is

<root> <foo> <main> <input1/> <input2/> <input3/> <input4/> <input5/> <inside/> <username/> <address/> </main> <main> <input1/> <input2/> <input3/> <input4/> <input5/> <inside/> <username>u1</username> <address/> </main> <main> <input1/> <input2/> <input3/> <input4/> <input5/> <inside/> <username>u2</username> <address/> </main> <main> <input1/> <input2/> <input3/> <input4/> <input5/> <inside/> <username>u1</username> <address/> </main> </foo> </root>

where username of first main empty because there 1 main username different other ones.

[edit] confused reply accepted, yet comment says need xslt 1.0 solution. provide xslt 1.0 stylesheet below:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="foo[main[username != ../main/username]]/main[1]/username"> <xsl:copy/> </xsl:template> </xsl:stylesheet>

xslt xslt-2.0

No comments:

Post a Comment