There are xml of this type:

<СуммаЗаМесяц> <Месяц>11</Месяц> <Год>2011</Год> <Выплат>2</Выплат> <Выплата> <Вид>Текст</Вид> <Сумма>10</Сумма> </Выплата> <Выплата> <Вид>Текст</Вид> <Сумма>15</Сумма> </Выплата> </СуммаЗаМесяц> <СуммаЗаМесяц> <Месяц>12</Месяц> <Год>2011</Год> <Выплат>2</Выплат> <Выплата> <Вид>Текст</Вид> <Сумма>10</Сумма> </Выплаты> <Выплата> <Вид>Текст</Вид> <Сумма>15</Сумма> </Выплаты> </СуммаЗаМесяц> 

All this should be output in the form of an HTML table, line by line, in two columns. The number of nodes " Amount for Month " and " Payment " can be arbitrary.
Tell me how to make a conclusion of all these nodes, if their number can change and they have the same name?

  • And what's the problem? - Pavel Mayorov
  • I understand, the question may look stupid on my part, but I'm new to xsl ... I can't figure out how to implement the output of like nodes if their number in the query can change. With what elements can this be achieved. - Kridshi
  • Is xslt necessary for you, or will another method (for example, XElement) work, which allows you to get a result? - Stack
  • We need exactly xslt template - Kridshi
  • and in which table is displayed? if you need to get an html-table (ie, tags table, tr, td) is one thing, and if displayed in the UI controls, another. - Stack

2 answers 2

 <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='html' encoding='utf-8' indent='yes' /> <xsl:template match='/'> <table> <xsl:for-each select='//Выплата'> <tr> <td><xsl:apply-templates select='Вид' /></td> <td><xsl:apply-templates select='Сумма' /></td> </tr> </xsl:for-each> </table> </xsl:template> <!-- правила для вывода тегов --> <xsl:template match='Сумма'><xsl:value-of select='.' /></xsl:template> <xsl:template match='Вид'><xsl:value-of select='.' /></xsl:template> </xsl:stylesheet> 

to run xslt in C #:

 using System.Xml.Xsl; using System.IO; using System.Xml; // ... static string Transform(string xslt, string xml) { var t = new XslCompiledTransform(); t.Load(new XmlTextReader(new StringReader(xslt))); var x = new XmlDocument(); x.LoadXml(xml); var w = new System.IO.StringWriter(); t.Transform(x.CreateNavigator(), null, w); return w.ToString(); } // ... string xslt = ...; string xml = ...; var r = Transform(xslt, xml); Console.WriteLine(r); 

approximate result format

 <table> <tr> <td>Текст</td> <td>10</td> </tr> <tr> <td>Текст</td> <td>15</td> </tr> </table> 
  • Thanks for the help! - Kridshi

Use for-each

 <table> <xsl:for-each select="//Выплата"> <tr> <td> <xsl:value-of select="Вид" /> </td> <td> <xsl:value-of select="Сумма" /> </td> </tr> </xsl:for-each> <table> 
  • When using for-each, I ran into such a problem: in the table under the node "SumMaMon Month" # 11 are displayed alternately "Payouts" and from other nodes "SumMaMonth" # 12, etc. Is it possible to somehow make a distinction between nodes so that there is no such confusion? Those. was so "Month # 11" - "Payments for the month # 11" | "Month # 12" - "Payments for the month # 12" - Kridshi
  • @Kridshi Of course you can. Put one cycle in another :) - Pavel Mayorov
  • @Kridshi is probably better to add this condition to the question, as well as add what the result should be (not html, but the result of the html output, that is, the table itself). - Stack