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

Objective C: Using spritesheet from TexturePacker without GameEngine
Write a tr069 emulate client with perl?
R programming XML: accessing duplicate node
Cannot find the declaration of element 'xs:schema'
Webservice schema validation not working
How to define XSD element to have multiple children, all of which are simple and only have attributes?
App Engine - Java Cache Control wont work
XML list types required for WCF
What is the meaning of xs:mixed without elements?
Copy xml element with whole tree
how to get unique record count for each location_Name by Record_No when RECORD_TYPE='ERROR' with XSLT
XML and accented charachters
XSLT grouping based on XML element values
REST and links: middle ground?
What's Haskell's attitude towards Unicode in XML?
Specific How-To Minimally Style XML with XSLT after adding over-arching categories

Categories

HOME
grok
facebook-oauth
github-for-windows
android-studio-2.2
interface
graphics
telegram-bot
opengl-3
reactive-programming
appcelerator-studio
plist
rtf
jsonpath
spring-social
gource
job-scheduling
google-picker
git-extensions
soci
sse
systemtime
parcelable
zxing
google-cast
powershell-ise
linkedhashmap
gojs
sql-delete
searchkit
swagger-php
latitude-longitude
parse-tree
dm-script
android-augmented-reality
setup.py
visa
importerror
pom.xml
httrack
textsum
podscms
google-cse
zenhub
singleinstance
android-testing
css-transforms
trigger.io
materialized-path-pattern
autosar
custom-lists
pacemaker
greatest-n-per-group
mapinfo
installshield-2010
google-query-language
android-wake-lock
yajsw
blitline
concept-insights
morton-number
vacuum
kubuntu
cosine-similarity
pseudo-class
human-computer-interface
raygun
ignite-ui
dot42
decoupling
dsym
workitem
nebula
epiceditor
fb.ui
pdfviewer
inkcanvas
isa-swizzling
entity-attribute-value
matlab-load
timthumb
unattended-processing
cloudfiles
ruby-1.8
instantiationexception
rowlex
binomial-heap
zend-rest
build-environment
wordprocessingml
windows-identity
sifr3

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