【问题标题】:How to extract strings from nodes having same name in xslt如何从xslt中具有相同名称的节点中提取字符串
【发布时间】:2017-04-24 07:40:05
【问题描述】:

我有 4 个具有 4 个不同值的相似节点。例如,我有以下值。

<sample>
        <a>123</a>
        <a>45</a>
        <a>67</a>
        <a>890</a>
</sample>

我需要检查每个节点的长度,如果它小于 3,那么应该像下面这样附加前导零。

<sample>
        <a>123</a>
        <a>045</a>
        <a>067</a>
        <a>890</a>
</sample>

添加前导零后,我需要将所有值连接在一起并将其作为单个字符串传递。

<a>123045067890</a>

请提供最佳解决方案。

【问题讨论】:

  • 所以一个好的解决方案是不够的,它必须是最优的?
  • 这在 XSLT 中很简单:只需在 xsl:for-each 指令中使用 format-number() 函数。
  • @DanielShillcock 是的,我已经尝试了一段代码。
  • @michael.hor257k 我使用了 xsl:for-each 命令。但是当我使用它时,只考虑第一个标签,而不考虑第二个和其他标签。
  • 这是我试过的代码。请提出你的想法。 错误

标签: c# asp.net xml xslt xsd


【解决方案1】:

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="sample">
    <a>
        <xsl:for-each select="a">
            <xsl:value-of select="format-number(., '000')" />
        </xsl:for-each>
    </a>
</xsl:template>

</xsl:stylesheet>

应用于您的示例输入,返回:

<?xml version="1.0" encoding="UTF-8"?>
<a>123045067890</a>

【讨论】:

    【解决方案2】:

    这是使用 XPath 的一种方法:

    string xml = @"
        <sample>
            <a>123</a>
            <a>45</a>
            <a>67</a>
            <a>890</a>
        </sample>
    ";
    
    //load xml
    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(xml);
    
    //get all <a> nodes and cast them to List<XmlNode>
    var nodes = xdoc.SelectNodes("sample/a")
        .Cast<XmlNode>().ToList();
    
    //iterate through each node and append leading zeroes until length is 3 chars
    nodes.ForEach(n => n.InnerText = n.InnerText.PadLeft(3, '0'));
    //join all values and add tags to beginning and the end
    string concatenatedValues = "<a>" + string.Join("", nodes.Select(x => x.InnerText).ToArray()) + "</a>";
    

    【讨论】:

      【解决方案3】:

      你可以试试这个:

      string xml = @"<sample>
                              <a>123</a>
                              <a>45</a>
                              <a>67</a>
                              <a>890</a>
                          </sample>";
      
      XDocument xmlDoc = XDocument.Parse(xml);
      int value = 0;
      StringBuilder errorsSb = new StringBuilder();
      List<string> a_Nodes = new List<string>();
      
      xmlDoc.Descendants("a").Select(x => x.Value).ToList().ForEach(x =>
      {
           if (int.TryParse(x, out value))
                a_Nodes.Add(value.ToString("D3"));
           else
                errorsSb.AppendLine($"Value {value} is not a number");
           });
      
           string res = String.Join(string.Empty, a_Nodes);
           if (!string.IsNullOrWhiteSpace(errorsSb.ToString()))
           {
                // handle errors
           }
      }
      

      【讨论】:

      猜你喜欢
      • 2018-06-20
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多