简体   繁体   English

使用函数检索节点集时如何显示它们?

[英]How can you display values within a node-set when retrieving them using a function?

I have a stylesheet called FourColumnTable that I want to use as a table template. 我有一个名为FourColumnTable的样式表,我想用作表模板。 At the moment it looks like this and uses the xml shown below it: 现在看起来像这样,并使用下面显示的xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl"http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:template name="FourColumnTable">
        <xsl:param name="rows" />

        <table>
            <tbody>
                <tr>
                    <td>Name</td>
                    <td>Age</td>
                    <td>Name</td>
                    <td>Age</td>
                </tr>
            </tbody>
        </table>

        <xsl:variable name="itemCount" select="count($rows/*)" />

        <xsl:if test="$itemCount &gt; 1">
            <xsl:for-each select="$rows/MyTableData">
                <table>
                    <tbody>
                        <tr>
                            <td><xsl:value-of select="name" /></td>
                            <td><xsl:value-of select="age" /></td>
                            <td>blank for now</td>
                            <td>blank for now</td>
                        </tr>
                    </tbody>
                </table>
            </xsl:for-each>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

The data is retrieved using a function that returns some xml as a string. 使用返回一些xml作为字符串的函数来检索数据。 The xml looks like this: xml看起来像这样:

<MyDataSet>
  <MyTableData>
    <name>Bob</name>
    <age>25</age>
  </MyTableData>
  <MyTableData>
    <name>Karen</name>
    <age>36</age>
  </MyTableData>
  <MyTableData>
    <name>Frank</name>
    <age>58</age>
  </MyTableData>
</MyDataSet>

I have an email template that currently works and that I now want to put this table into it. 我有一个目前可以使用的电子邮件模板,现在我想将此表放入其中。 So at the moment I'm doing the following: 因此,目前我正在执行以下操作:

<xsl:template name="CustomerData">
  <xsl:call-template name="FourColumnTable">
    <xsl:with-param name="rows" select="msxml:node-set(myFunctions:GetCustomerData())" />
  </xsl:call-template>
</xsl:template>

At the moment this seems to create a node-set, but when I try and use it in the for-each it doesn't show any values. 目前,这似乎创建了一个节点集,但是当我尝试在for-each使用它时,它不显示任何值。 I've tried removing the if statement and then doing the following: 我试过删除if statement ,然后执行以下操作:

  • for the for-each select statement changing it to "$rows/MyDataSet/*". 对于for-each select语句,将其更改为“ $ rows / MyDataSet / *”。
  • for the for-each select statement changing it to "$rows/MyDataSet/MyTableData". 对于for-each select语句,将其更改为“ $ rows / MyDataSet / MyTableData”。
  • for the for-each select statement changing it to "$rows/node()" and then in the first column for the select statement of the 'value-of' adding "$rows/node()". 对于for-each select语句,将其更改为“ $ rows / node()”,然后在“ value-of”的select语句的第一列中添加“ $ rows / node()”。 This displayed all of the data as per the xml, so I know $rows at least has the data. 这将根据xml显示所有数据,因此我知道$ rows至少具有数据。

I've tried variations of the above but those seem to be the main tests worth mentioning. 我已经尝试了上述方法的变体,但是这些似乎是值得一提的主要测试。 What I can't understand is why it will return the data for everything, but I'm unable to loop through for specific nodes (MyTableData nodes). 我不明白的是为什么它将返回所有数据,但是我无法遍历特定节点(MyTableData节点)。 Help please. 请帮助。

UPDATE 更新

Here is my function GetCustomerData() : 这是我的函数GetCustomerData()

public string GetCustomerData()
{
    var sb = new StringBuilder();
    var xmlDoc = new XmlDocument();

    xmlDoc.LoadXml(this.GetAllCustomerData());

    var nodeList = xmlDoc.SelectNodes("/MyTableData");

    if (nodeList != null && nodeList.Count > 0)
    {
        foreach (XmlNode node in nodeList)
        {
            sb.Append(node.InnerText + EnvironmentNewLine);
        }
    }

    return sb.ToString();
}

I'm guessing this needs to return an XmlDocument instead? 我猜想这需要返回XmlDocument吗? However, my GetAllCustomerData method returns a lot more than what I need, which is why I select the nodes I want. 但是,我的GetAllCustomerData方法返回的值比我需要的要多得多,这就是为什么我选择所需的节点的原因。 Can I take this XmlNodeList and turn that into an XmlDocument? 我可以将此XmlNodeList转换为XmlDocument吗?

UPDATE 2 更新2

I opted to change my function to simply return the xml document as is and then in my table template alter what nodes I iterate over within the foreach statement like this: 我选择更改函数以按原样返回xml文档,然后在表模板中更改在foreach语句中迭代的节点,如下所示:

public XmlDocument GetCustomerData()
{
    var xmlDoc = new XmlDocument(); 
    xmlDoc.LoadXml(this.GetAllCustomerData());

    return xmlDoc;
}

This allowed me to iterate over what I needed. 这使我可以迭代所需的内容。

It looks like your custom function is not returning an xml document or document fragment, it's probably just a string, but without being able to see that function I can't say for sure. 看来您的自定义函数没有返回xml文档或文档片段,它可能只是一个字符串,但无法看到我不确定的函数。

In other words, you've got the equivalent of 换句话说,您具有

<xsl:param name="rows">&lt;MyDataSet&gt;Data&lt;/MyDataSet&gt;</xsl:param>

instead of 代替

<xsl:param name="rows"><MyDataSet>Data</MyDataSet></xsl:param>

And you're not able to process is as XML, because it's not, it's just the markup. 而且您不能像XML那样处理,因为不是,它只是标记。 Calling node-set on it doesn't parse it into XML, it turns it into a set of one text node. 在其上调用节点集不会将其解析为XML,而是将其转换为一组一个文本节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM