XSLT display ALL XML tag contents

天涯浪子 提交于 2019-12-04 03:50:20

问题


I am new to using XSLT. I want to display all of the information in an xml tag in my xsl formatted page. I have tried using local-name, name, etc and none give me the result I want.

Example:

 <step bar="a" bee="localhost" Id="1" Rt="00:00:03" Name="hello">Pass</step>

I would like to be able to print out all of the information (bar="a", bee="localhost") etc as well as the value of <step> Pass.

How can I do this with xsl?

Thank you!


回答1:


If you want to return just the values, you could use the XPath //text()|@*.

If you want the attribute/element names along with the values, you could use this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="*">
    <xsl:apply-templates select="node()|@*"/>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:value-of select="concat('&lt;',name(parent::*),'> ',.,'&#xA;')"/>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:value-of select="concat(name(),'=&#x22;',.,'&#x22;&#xA;')"/>
  </xsl:template>

</xsl:stylesheet>

With your input, it will produce this output:

bar="a"
bee="localhost"
Id="1"
Rt="00:00:03"
Name="hello"
<step> Pass



回答2:


<xsl:for-each select="attribute::*">   
  <xsl:value-of select="text()" />   
  <xsl:value-of select="local-name()" />   
</xsl:for-each>
<xsl:value-of select="text()" />   
<xsl:value-of select="local-name()" />   


来源:https://stackoverflow.com/questions/7518717/xslt-display-all-xml-tag-contents

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!