xml


Generic xslt that transform xml structure in html list


I need a generic xslt that transform xml to an html list.
Example xml snippet:
<person>
<firstname>A</firstname>
<lastname>B</lastname>
<address>
<city>C</city>
<street>S</street>
</address>
</person>
The output should be
<ul>
<li>person
<ul>
<li>firstname: A</li>
<li>lastname: B</li>
<li>address
<ul>
<li>city: C</li>
<li>street: S</li>
</ul>
</ul>
</li>
</ul>
The source xml is generated at runtime an will not always have this structure. So the tag names can be different. Therfore the xslt has to be generic.
try something like:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="/">
<ul>
<xsl:apply-templates select="*"/>
</ul>
</xsl:template>
<xsl:template match="*[*]">
<li>
<xsl:value-of select="name()"/>
<ul>
<xsl:apply-templates select="*"/>
</ul>
</li>
</xsl:template>
<xsl:template match="*[not(*)]">
<li>
<xsl:value-of select="concat(name(), ': ', .)"/>
</li>
</xsl:template>
</xsl:stylesheet>
How about:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="*">
<li>
<xsl:value-of select="name()" />
<xsl:if test="text()">
<xsl:text>: </xsl:text>
<xsl:apply-templates select="text()" />
</xsl:if>
<xsl:if test="*">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>

Related Links

Talend - getCode() error while extracting XML file
value-of select=“document($path)/ns0:element1/ns0:element2” returns NULL with SAXON-B XSLT 2.0 (saxon9)
XSD extend base with suppression of one attribute
ServiceStack Response - Change encoding?
Wrapping multiple identically named elements into a new element using XSLT
Xml schema uniqueness in entire subtree
dynamic xml nodes using xslt
xpst0003 xquery syntax error
Xsl anchor attribute
How to get start and end of a week of a specific date using xslt?
how to select elements when the root element has a lot of attributes
xsd select multiple values from enumeration or equivalent type
Issue with XSL loops and linefeeds
what is the xpath of this element
Openerp module output not generated properly
trasfer one excel sheet into multiple xml content in mule

Categories

HOME
service-worker
shell
string
toolbox
interface
jelastic
gradient
aws-cli
crystal-reports-2013
mvc5
crystal-lang
trac
bing-maps
mql5
distribution
libc
parameter-passing
screen-readers
powershell-v5.0
google-container-registry
cfml
lilypond
gulp-watch
aggregation
froala
tiki-wiki
jackson-modules
mat
android-things
subclass
errbot
realm-mobile-platform
boost-asio
hard-drive
mnist
servicestack-text
xajax
vmware-fusion
preloader
sharp-snmp
contract
android-exoplayer
opencmis
scalding
edit
monkey
flac
py2neo
mathcad
shinobi
restore
vcloud-director-rest-api
slackware
stax
lrs
google-cloud-console
dotnetnuke-7
windows-azure-queues
gcloud-ruby
ikiwiki
ellucian-scribe
false-sharing
sapi
baasbox
sql-server-data-tools
mirror
bufferedinputstream
zend-currency
android-broadcast
showdialog
pseudo-class
sip-server
sql-scripts
strawberry-perl
xml-namespaces
jqmodal
ss7
authlogic
square-bracket
notorm
light
jammer
c++builder-xe5
asdf
dolby-audio-api
zope.interface
adobe-media-server
jsr286
winmain
listactivity
creole
msr
codebase
objective-j
zen
pos-for-.net
burndowncharts
silverlight-oob
alsb
int64
firefox-3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App