You haven't shown us how $selectedPerson
is defined, but I would suggest trying this to pass a set of the individual nodes to the function:
<xsl:value-of
select="custom:SelectPerson($selectedPerson//*[not(*)])"/>
質問
In my XML, I have something like this:
<people>
<person>
<name>
<first>Bob</first>
<last>Bobbertson</last>
</name>
<age>80</age>
</person>
...
</people>
I want to be able to pass the entire <person>
section to my code-behind so that in my C# code, I can have access to all of the subnodes (<name>
, <first>
, <last>
).
I am using XSLT to generate my HTML, and in my XSLTfile, I have a line like this:
<xsl:value-of select="custom:SelectPerson($selectedPerson)"/>
Which calls this in my code-behind:
public string SelectPerson(System.Xml.XPath.XPathNodeIterator selectedPersonNodes)
{
foreach (var node in selectedPersonNodes)
{
Debug.WriteLine(node.ToString());
}
...
}
What I want to happen is to have the foreach
loop three times, and print out "Bob", then "Bobbertson", then "80". But what is happening is that it goes through once and prints out "BobBobbertson80".
Is there a way to pass the nodes in the way I want from the XSLT-generated HTML to my code-behind?
解決
You haven't shown us how $selectedPerson
is defined, but I would suggest trying this to pass a set of the individual nodes to the function:
<xsl:value-of
select="custom:SelectPerson($selectedPerson//*[not(*)])"/>
他のヒント
Note that the C# code below is untested as I don't have access to a C# development environment.
What your C# code is doing is correct. You are passing an iterator that contains just one node. (Examine selectedPersonNodes.Count
to check). That node is the element <person>
which, as you say, has descendants, but it is still a single node.
Assuming your XPathNodeIterator
can ever contain multiple person
elements, you should write
foreach (var person in selectedPersonNodes)
{
Debug.WriteLine(person.SelectSingleNode("name/first").ToString());
Debug.WriteLine(person.SelectSingleNode("name/last").ToString());
Debug.WriteLine(person.SelectSingleNode("age").ToString());
}
or if it always contains a single person, you can say
selectedPersonNodes.MoveNext();
XPathNavigator person = selectedPersonNodes.Current;
Debug.WriteLine(person.SelectSingleNode("name/first").ToString());
Debug.WriteLine(person.SelectSingleNode("name/last").ToString());
Debug.WriteLine(person.SelectSingleNode("age").ToString());
but, as I said, you may want to check the value of selectedPersonNodes.Count
before you do either of these.