xml - Iterate and grab nodes by position -
i want copy xml nodes in repeating structure repeating structure position...meaning first node going first first node , on..
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:header/> <soap-env:body> <n0:ycaxmlporesponse xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style"> <xmlerroritmout> <item> <yyxmllineitem>000000</yyxmllineitem> <yyextpoitem>000001</yyextpoitem> <yylang/> <yyexterror>1</yyexterror> <yyerrordesc>2</yyerrordesc> </item> <item> <yyxmllineitem>000000</yyxmllineitem> <yyextpoitem>000002</yyextpoitem> <yylang>en</yylang> <yyexterror>3</yyexterror> <yyerrordesc>4</yyerrordesc> </item> </xmlerroritmout> <xmlitem> <item> <mandt>430</mandt> <yypartnerid>1100021413304</yypartnerid> <yyxmldocid>us-coc-104492-19311128</yyxmldocid> <yypolineitem>000001</yypolineitem> </item> <item> <mandt>430</mandt> <yypartnerid>1100021413304</yypartnerid> <yyxmldocid>us-coc-104492-19311128</yyxmldocid> <yypolineitem>000002</yypolineitem> </item> </xmlitem> </n0:ycaxmlporesponse> </soap-env:body> into following strucure
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:header/> <soap-env:body> <n0:ycaxmlporesponse xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style"> <xmlerroritmout> <item> <yyxmllineitem>000000</yyxmllineitem> <yyextpoitem>000001</yyextpoitem> <yylang/> <yyexterror>1</yyexterror> <yyerrordesc>2</yyerrordesc> </item> <item> <yyxmllineitem>000000</yyxmllineitem> <yyextpoitem>000002</yyextpoitem> <yylang>en</yylang> <yyexterror>3</yyexterror> <yyerrordesc>4</yyerrordesc> </item> </xmlerroritmout> <xmlitem> <item> <mandt>430</mandt> <yypartnerid>1100021413304</yypartnerid> <yyxmldocid>us-coc-104492-19311128</yyxmldocid> <yypolineitem>000001</yypolineitem> <yyexterror>1</yyexterror> <yyerrordesc>2</yyerrordesc> </item> <item> <mandt>430</mandt> <yypartnerid>1100021413304</yypartnerid> <yyxmldocid>us-coc-104492-19311128</yyxmldocid> <yypolineitem>000002</yypolineitem> <yyexterror>3</yyexterror> <yyerrordesc>4</yyerrordesc> </item> </xmlitem> </n0:ycaxmlporesponse> </soap-env:body> help appreciated!..
try this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:key name="error" match="xmlerroritmout/item" use="count(preceding-sibling::item) + 1"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="xmlitem/item"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> <xsl:copy-of select="key('error', position())/yyexterror"/> <xsl:copy-of select="key('error', position())/yyerrordesc"/> </xsl:copy> </xsl:template> </xsl:stylesheet> first create key. use count preceding sibling nodes instead of position. although may slow if had thousands of item, works.
the identity transform copies every node "as-is".
the second template matches xmlitem/item element, copy content , add error elements key.
Comments
Post a Comment