首页 > 解决方案 > 当属性具有'@'时如何在 XSL 中获取值

问题描述

如何在下面的XSL文件中获取<xsl>元素的值?'maa'

<xsl:stylesheet version="3.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eu="http://europa.eu.int" xmlns:xlink="http://www.w3c.org/1999/xlink"> 
   <xsl:template mode="agency"> 
    <xsl:choose>
        <xsl:when test="@code='AT-BASG'">Austria - BASG- Austrian Federal Office for Safety in Health Care / Austrian Medicines and Medical Devices Agency</xsl:when>
     </xsl:choose>
   </xsl:template>
   <xsl:template mode="submission">
    <xsl:choose>
        <xsl:when test="@type='maa'">Marketing Authorisation</xsl:when>
    </xsl:choose>
   </xsl:template>
</xsl:stylesheet>   

我尝试如下:

        string emp = "@type='maa'";
        XmlDocument xslDoc = new XmlDocument();
        xslDoc.Load(IndexFTPLocation);
        //ReadXElement(indexXele, sequenceName, ApplicationName, IndexFTPLocation, 1);
        XmlNamespaceManager nsMgr = new XmlNamespaceManager(xslDoc.NameTable);
        nsMgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
        XmlNode usrNode = xslDoc.SelectSingleNode("/xsl:stylesheet/xsl:template[@mode='submission']/xsl:choose/xsl:when[@test='@type='maa'']", nsMgr);  

但是,当 type="maa" 时,我无法获得“营销授权”。你能帮我解决这个问题吗?

提前致谢!!

编辑: 出现错误:'/xsl:stylesheet/xsl:template[@mode='submission']/xsl:choose/xsl:when[@test='@type='maa'']' 具有无效标记。

标签: c#xmlxslt

解决方案


您可以使用in"代替,如以下代码:'xsl:when[@test='@type='maa'']

XmlDocument xslDoc = new XmlDocument();
xslDoc.Load(IndexFTPLocation);

XmlNamespaceManager nsMgr = new XmlNamespaceManager(xslDoc.NameTable);
nsMgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");

XmlNode usrNode = xslDoc.SelectSingleNode("/xsl:stylesheet/xsl:template[@mode='submission']/xsl:choose/xsl:when[@test=\"@type='maa'\"]", nsMgr);
string text = usrNode?.InnerText;

演示

Console.WriteLine(text);

结果

Marketing Authorisation

我希望你觉得这有帮助。


推荐阅读