xslt - convert XML formats -


i have xml format below in string:

  <?xml version="1.0" encoding="utf-8" ?>  - <soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> - <soap:body> - <getuserlinksresponse xmlns="http://microsoft.com/webservices/sharepointportalserver/userprofileservice"> - <getuserlinksresult> - <quicklinkdata>   <name>system 1</name>    <group>general</group>    <privacy>private</privacy>    <url>https://url1.com</url>    <id>6500</id>    </quicklinkdata> - <quicklinkdata>   <name>system 2</name>    <group>general</group>    <privacy>private</privacy>    <url>https://url2.com</url>    <id>6499</id>    </quicklinkdata> - <quicklinkdata>   <name>system 3</name>    <group>work</group>    <privacy>private</privacy>    <url>http://url3.com</url>    <id>6845</id>    </quicklinkdata>   </getuserlinksresult>   </getuserlinksresponse>   </soap:body>   </soap:envelope> 

which want in format below using xslt or in c#:

<?xml version="1.0" encoding="utf-8" ?>  <root> <group> <value>general</value> <item>  <id>6500</id>  <name>system 1</name>  <url>https://url1.com</url> </item> <item>  <id>6499</id>  <name>system 2</name>  <url>https://url2.com</url> </item> </group> <group> <value>work</value> <item>  <id>6845</id>  <name>system 3</name>  <url>https://url3.com</url> </item> </group> </root> 

there multiple groups how can achieve in c# (using xslt or other process)

please help...

this short , simple transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"  xmlns:x="http://microsoft.com/webservices/sharepointportalserver/userprofileservice"  exclude-result-prefixes="x">  <xsl:output omit-xml-declaration="yes" indent="yes"/>   <xsl:key name="kgroupbyval" match="x:group" use="."/>   <xsl:template match=    "x:group[generate-id() = generate-id(key('kgroupbyval',.)[1])]">   <group>    <value><xsl:value-of select="."/></value>    <xsl:apply-templates select="key('kgroupbyval',.)/.." mode="ingroup"/>   </group>  </xsl:template>   <xsl:template match="x:quicklinkdata" mode="ingroup">   <item>    <id><xsl:value-of select="x:id"/></id>    <name><xsl:value-of select="x:name"/></name>    <url><xsl:value-of select="x:url"/></url>   </item>  </xsl:template>  <xsl:template match="text()"/> </xsl:stylesheet> 

when applied on provided xml document:

<soap:envelope  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"  xmlns:xsd="http://www.w3.org/2001/xmlschema">     <soap:body>         <getuserlinksresponse           xmlns="http://microsoft.com/webservices/sharepointportalserver/userprofileservice">             <getuserlinksresult>                 <quicklinkdata>                     <name>system 1</name>                     <group>general</group>                     <privacy>private</privacy>                     <url>https://url1.com</url>                     <id>6500</id>                 </quicklinkdata>                 <quicklinkdata>                     <name>system 2</name>                     <group>general</group>                     <privacy>private</privacy>                     <url>https://url2.com</url>                     <id>6499</id>                 </quicklinkdata>                 <quicklinkdata>                     <name>system 3</name>                     <group>work</group>                     <privacy>private</privacy>                     <url>http://url3.com</url>                     <id>6845</id>                 </quicklinkdata>             </getuserlinksresult>         </getuserlinksresponse>     </soap:body> </soap:envelope> 

produces wanted, correct result:

<group>    <value>general</value>    <item>       <id>6500</id>       <name>system 1</name>       <url>https://url1.com</url>    </item>    <item>       <id>6499</id>       <name>system 2</name>       <url>https://url2.com</url>    </item> </group> <group>    <value>work</value>    <item>       <id>6845</id>       <name>system 3</name>       <url>http://url3.com</url>    </item> </group> 

explanation:

proper use of muenchian grouping method.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -