遍历 xslt 中的字符串列表,并使用索引

Iterating over list of strings in xslt, and using index

我在 C# 工作。 我使用 XSL 转换 (XSLT) 1.0 版。 https://www.w3.org/TR/xslt 我使用 xslt 从 class MyClass 中的数据创建 xml MyClass 与模板匹配并具有 属性

 List<string> Strings

我需要这样导出:

<Line1> first string value Here </Line1>
<Line2> Second string value here </Line2>
.
.
.

有人可以帮我实现吗? 祝一切顺利 塔尔

很简单:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication16
{
    class Program
    {

        static void Main(string[] args)
        {
            List<string> Strings = new List<string>() {
                    "first string value Here",
                    "Second string value here"
            };

            List<XElement> output = Strings.Select((x, i) => new XElement("Line" + (i + 1).ToString(), x)).ToList(); 
        }

    }

}

我找到了解决办法。也许有更好的... 我创建了一个新的 ExportClass:

public class ExportString
{
   [XmlElement("StringObject")]
   public string StringObject{get; set;}
}

我创建了一个列表,因为 属性 是与 XSLT 匹配的 MyExportClass:

public class MyExportClass
{
   [XmlElement("LOS"]
   public List<StringObject> LOS<get; set;}
}

我在 XSLT 中使用过这种语法: 该代码在元素名称中包含索引:

<xsl:for-each select="LOS" xml:space="default">

 //Get the index
  <xsl:variable name ="index" select="position()"/>

  <xsl:element name ="Line{$index}">
  <xsl:value-of select = "StringObject"/>
  </xsl:element>

   //Line break
  <xsl:text> &#xa;</xsl:text>
</xsl:for-each>

结果:

<Line1> my first string </Line1>
<Line2> my second string </Line2>
.
.
.