XML XSL get属性

时间:2017-12-19 19:01:07

标签: xml xslt

我有类似下面的xml文件。我需要使用xsl获取成员名称。

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="styleSheet.xsl"?>
<doc>
    <assembly>
        <name>AlienRFID2</name>
    </assembly>
    <members>
        <member name="T:nsAlienRFID2.AlienDataDirector">
            <summary>
            EXPERIMENTAL.  Objects of this class may be used for sending Alien reader's messages to an external listener.
            Typical use of this class is by a mobile device, which is listening for Alien reader's notifications using CAlienServer objects and 
            wants to transfer those to a host (target) computer.
            </summary>
        </member>
        <member name="M:nsAlienRFID2.AlienDataDirector.#ctor(System.Int32,System.Net.IPAddress,System.Boolean)">
            <summary>
            Creates instance for connecting to specified host server.
            </summary>
            <param name="targetPort">Network port value for connecting to host (target.)</param>
            <param name="targetIPAddress">IPAddress value for connecting to host (target.)</param>
            <param name="log">TRUE for turning internal API logging ON, FALSE otherwise.</param>
        </member>
    </members>
</doc>

我需要吐出&#39; T:nsAlientFRID2.Alien ......&#39;和&#;;;;;;;;;;;;;;;;;;;;;;;;; &#34;会员名称&#34;字符串到浏览器。我理解它像xsl:value-of select =&#34; member / @ name&#34;但是我无法对此进行任何变化。我无法找到如何在引号中获取属性的明确示例。有人可以用一个例子来帮助我吗?谢谢!

编辑..我目前正在尝试使用此代码,但它会显示摘要,而不是属性&#39; name&#39;。

 <xsl:for-each select = "doc/members/member">             
      <xsl:value-of select = "@name"/>           
 </xsl:for-each> 

1 个答案:

答案 0 :(得分:1)

您可以使用与该元素匹配的名为styleSheet.xsl的样式表,然后使用xsl:value-of获取该属性的值:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="doc/members/member">
    <xsl:value-of select="@name" /><br />
  </xsl:template>

  <!-- suppress other text nodes -->
  <xsl:template match="text()" />

</xsl:stylesheet>

然后您可以根据需要格式化输出。

<强>输出:

T:nsAlienRFID2.AlienDataDirector
M:nsAlienRFID2.AlienDataDirector.#ctor(System.Int32,System.Net.IPAddress,System.Boolean)