【问题标题】:Retrieve src value from xmi files从 xmi 文件中检索 src 值
【发布时间】:2015-01-07 09:05:18
【问题描述】:

我有一组 xml 文件,我必须从中检索 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 的值。

问候, 基兰

【问题讨论】:

  • xmi 文件格式如下: ...

标签: xslt


【解决方案1】:

使用 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>

【讨论】:

    【解决方案2】:

    你可以像这样直接访问任何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);
    

    }

    } }

    【讨论】:

    • img 属性不在根节点。它可以存在于任何节点中。节点名称和节点的层次结构因文件而异。有没有办法从所有 xmi 文件中获取 src 属性,而不管节点结构如何?
    猜你喜欢
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2018-02-15
    相关资源
    最近更新 更多