1

私は次のXMLを持っています。

<Programme>
    <Intakes>
        <One>
            <Information />
        </One>
        <Two>
            <Information />
        </Two>
    </Intakes>
</Programme>

リストボックス内の必要な結果情報:

One
Two

基本的に、各摂取量(1、2など)のオプションをリストボックスに入力したいと思います。

複数回発生することはありません。

だから私は個々の摂取量の子ノードを取得しませんか?

現在のコード:

        XPathNavigator nav;
        XPathDocument docNav;
        XPathNodeIterator NodeIter;
        string strExpression;

        docNav = new XPathDocument(docPath);
        nav = docNav.CreateNavigator();

        strExpression = "//Intakes/node()";
        NodeIter = nav.Select(strExpression);

        while (NodeIter.MoveNext())
        {
            lstIntakes.Items.Add(NodeIter.Current.Value);
        }

ただし、これにより、ノード内からすべてのxmlを含むリストボックスに1つのアイテムのみが追加されます。

4

1 に答える 1

4

XPath is a query language for XML documents and as such it cannot alter the structure of a document.

XSLT is a proper tool for transforming an XML document into another. The XSLT solution of this problem is:

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

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/*"><xsl:apply-templates/></xsl:template>
 <xsl:template match="One/node()|Two/node()"/>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<Programme>
    <Intakes>
        <One>
            <Information />
        </One>
        <Two>
            <Information />
        </Two>
    </Intakes>
</Programme>

the wanted, correct result is produced:

<Intakes>
   <One/>
   <Two/>
</Intakes>

UPDATE: The OP has changed the question radically -- now the wanted result is:

One  
Two 

This still cannot be provided by a single XPath 1.0 expression (and as you are using C#, you probably don't have access to an XPath 2.0 implementation).

You have first to select all children elements of Intakes:

/*/Intakes/*

Then you have to iterate through the returned node-set and for each element contained in it evaluate this XPath expression:

name()
于 2012-06-22T12:55:10.387 に答える