2015-01-07 29 views
0

我有一组xml文件,我必须从中检索src后存在的值。从xmi文件检索src值

实施例:

<?xml version="1.0" encoding="UTF-8"?> 
<org.eclipse.epf.uma:ContentDescription xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:org.eclipse.epf.uma="http://www.eclipse.org/epf/uma/1.0.6/uma.ecore" xmlns:rmc="http://www.ibm.com/rmc" rmc:version="7.5.1" xmlns:epf="http://www.eclipse.org/epf" epf:version="1.5.1" xmi:id="-ES_igec88m8mZhEXekVK3A" name="fs_evms,_zl9y8FCTEd6XXocT9rJgNQ" guid="-ES_igec88m8mZhEXekVK3A" changeDate="2009-08-01T13:54:29.422-0400" version="7.5.0"> 
    <mainDescription>&lt;p>&#xD; 
    &lt;img alt=&quot;&quot; src=&quot;resources/IPMS_As-Is_75.jpg&quot; width=&quot;587&quot; height=&quot;346&quot; />&#xD; 
&lt;/p></mainDescription> 
</org.eclipse.epf.uma:ContentDescription> 

<sections xmi:id="_vlul8AKaEd6N9prBktGuYg" name="Update the opportunity information in Siebel" guid="_vlul8AKaEd6N9prBktGuYg"> 
    <sectionDescription>&lt;img alt=&quot;&quot; src=&quot;resources/UpdateOpportunity.JPG&quot; width=&quot;597&quot; height=&quot;360&quot; /></sectionDescription> 
    </sections> 

非常从一个XML文件到另一个节点的层次结构和值。请让我知道如何使用xslt从上述数据中检索src的值。

问候, 基兰

+0

XMI文件格式是象下面这样: ... <之前的图像格式为&lt;引号格式为"格式。 –

回答

0

使用XSLT严重与你的输入限制,因为转义标记不是XML。需要使用字符串函数以提取所需的部分 - 例如:

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:template match="/"> 
    <output> 
     <xsl:for-each select="//*[contains(text(), 'src=&quot;')]"> 
      <url> 
       <xsl:value-of select="substring-before(substring-after(., 'src=&quot;'), '&quot;')"/> 
      </url> 
     </xsl:for-each>  
    </output> 
</xsl:template> 

</xsl:stylesheet> 
0

您可以直接访问任何XML文件这样的..

using System; 
using System.IO; 
using System.Xml; 

public class Sample 
{ 
public static void Main() 
{ 

XmlDocument doc = new XmlDocument(); 
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + 
      "<title>Pride And Prejudice</title>" + 
      "</book>"); 

XmlElement root = doc.DocumentElement; 

// Check to see if the element has a genre attribute. 
if (root.HasAttribute("genre")){ 
    String genre = root.GetAttribute("genre"); 
    Console.WriteLine(genre); 

}

}}

+0

请参阅http://stackoverflow.com/questions/18017692/c-sharp-get-values-from-xml-attributes –

+0

img属性不在根节点中。它可以出现在任何节点中。节点名称和节点的层次结构因文件而异。有没有办法从所有的xmi文件中获取src属性,而不管节点结构如何? –