XSL characterize the arbitrary number of nodes



  • There are xml of this kind:

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

    All this has to be taken out as HTML tablets, fast, two pillars. Number of knotsAmountAndPayment"May be arbitrary.
    Can you tell me how to draw the conclusion of all these knots if their number can change and they have the same names?



  • <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>
    

    for 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);

    Example

    <table>
    <tr>
    <td>Текст</td>
    <td>10</td>
    </tr>
    <tr>
    <td>Текст</td>
    <td>15</td>
    </tr>
    </table>


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2