2012-06-08 43 views
0

我有这样的XML:Xpath的选择逃跑节点与节点

<text> 
    blah blah &lt;strong&gt; hello &lt;/strong&gt; more text &lt;strong&gt;hello again&lt;/strong&gt; blah blah 
</text> 

如何选择已逃脱&lt&gt

在这个例子中,选择应强标签内的文本是:

  1. 你好
  2. 您好再次

更新必须XSLT 1.0

+0

有没有“强有力的标签”没有标记(单元或其它节点除了名为'text'元素的单个文本子节点。因此,这个问题与XPath或XSLT无关,需要重新解析被破坏的标记,然后才能使用XPath(在XSLT内部或外部)。 –

回答

0

既然你已经更新了说你只能使用XSLT 1 - 看到这个职位:How to use XSLT 1.0 or XPath to manipulate an HTML string

这是一个有点复杂,但:

要代替<,>和&您必须将其清洁三次...

这里有一些XSLT让你开始:

<xsl:variable name="cleanXML"> 
    <xsl:call-template name="SubstringReplace"> 
    <xsl:with-param name="stringIn"> 
     <xsl:call-template name="SubstringReplace"> 
     <xsl:with-param name="stringIn"> 
      <xsl:call-template name="SubstringReplace"> 
      <xsl:with-param name="stringIn"> 
       <xsl:call-template name="SubstringReplace"> 
       <xsl:with-param name="stringIn" select="$theXml"/> 
       <xsl:with-param name="substringIn" select="'&amp;lt;'"/> 
       <xsl:with-param name="substringOut" select="'&lt;'"/> 
       </xsl:call-template> 
      </xsl:with-param> 
      <xsl:with-param name="substringIn" select="'&amp;gt;'"/> 
      <xsl:with-param name="substringOut" select="'&gt;'"/> 
      </xsl:call-template> 
     </xsl:with-param> 
     <xsl:with-param name="substringIn" select="'&amp;amp;'"/> 
     <xsl:with-param name="substringOut" select="'&amp;'"/> 
     </xsl:call-template> 
    </xsl:with-param> 
    </xsl:call-template> 
</xsl:variable> 
0

这里是一个C#实现。

命名空间中使用

using System.Xml 
using System.Web 

实施

 //Read xml file 
    string xmlText = "<text>blah blah &lt;strong&gt; hello &lt;/strong&gt; more text &lt;strong&gt;hello again&lt;/strong&gt; blah blah</text>"; 
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 
    doc.LoadXml(HttpUtility.HtmlDecode(xmlText)); 
    XmlNodeList Nodes = doc.GetElementsByTagName("strong"); 

    List<string> nodeValues= new List<string>(); 
    foreach (XmlNode Node in Nodes) 
    { 
     nodeValues.Add(Node.InnerText); 
    }