开发者

XSLT: How can I get the names of all nodes?

I'm looking for a stylesheet which prints the name of each node including i开发者_运维百科ts value. Getting the value is easy but I don't know how to get the name of each node. Here's the basic template:

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" encoding="UTF-8" />   
    <xsl:template match="/">
      <xsl:for-each select="/">
         ???
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Could anyone give me a hint?

Thanks, Robert


This complete transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:variable name="vQ">"</xsl:variable>

 <xsl:template match="*">
     <xsl:value-of select=
       "concat(name(), ' = ', $vQ, ., $vQ, '&#xA;')"/>
    <xsl:apply-templates select="*"/>
 </xsl:template>
</xsl:stylesheet>

when applied on any XML document, such as this:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

produces exactly the wanted, correct result:

nums = "01020304050607080910"
num = "01"
num = "02"
num = "03"
num = "04"
num = "05"
num = "06"
num = "07"
num = "08"
num = "09"
num = "10"


I don't know what you really want, but if I take your requirement literally, it is met by:

<xsl:for-each select="//node()">
  <node name="{name()}" value="{string(.)}"/>
</xsl:for-each>


Name(), "Returns the name of the current node or the first node in the specified node set"

    <xsl:value-of select="name()"/> 

Also,

local-name() - "Returns the name of the current node or the first node in the specified node set - without the namespace prefix"

And I think you want this,

 <xsl:value-of select="local-name()"/>


Try:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" encoding="UTF-8"/>
    <xsl:template match="/">
        <xsl:for-each select="//*">
            <xsl:value-of select="local-name()"/>=<xsl:apply-templates select="current()/text()"/>
            <xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜