简体   繁体   中英

How to get the value of an attribute of the root element in XSLT?

I have this XML :

 <?xml version="1.0" encoding="utf-8" ?>
 <IMPORT mode="FULL">
    ....
 </IMPORT>

I'm trying to convert it with the following XSLT stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:element name="import">
      <xsl:attribute name="mode">
        <xsl:value-of select="@mode"/>
      </xsl:attribute>
           ....
      </xsl:element>
  </xsl:template>

My problem is that the following line doesn't seem to work:

<xsl:value-of select="@mode"/>

As I only get

<import mode="">

instead of the expected:

<import mode="FULL">

Any clues ?

You're not actually on the IMPORT element.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:apply-templates select="IMPORT"/>
  </xsl:template>
  <xsl:template match="IMPORT">
    <xsl:element name="import">
      <xsl:attribute name="mode">
        <xsl:value-of select="@mode"/>
      </xsl:attribute>
       ....      
    </xsl:element>
  </xsl:template>

尝试使用<xsl:value-of select="IMPORT/@mode"/>

Try this:

<xsl:template match="IMPORT">
    <xsl:element name="import">
        <xsl:attribute name="mode">
            <xsl:value-of select="@mode"/>
            </xsl:attribute>
    </xsl:element>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM