xml


Check for null value and Ignore the tag XSLT


I have an xml like below:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
<entry key="user">1234</entry>
<entry key="name"></entry>
</properties>
I want to check if the value of the key "name" for null, if null then ignore the complete tag and the result xml should look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
<entry key="cm:user">1234</entry>
</properties>
If not null then the result xml should look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
<entry key="cm:user">1234</entry>
<entry key="cm:name">sam</entry>
</properties>
I am using the below xslt but not getting the desired output.
<xsl:template match="#key[.='user']">
<xsl:attribute name="key">
<xsl:value-of select="'cm:user'"/>
</xsl:attribute>
</xsl:template>
<xsl:choose>
<xsl:when test="#key[.='name'] != ''">
<xsl:template match="#key[.='name']">
<xsl:attribute name="key">
<xsl:value-of select="'cm:name'"/>
</xsl:attribute>
</xsl:template>
</xsl:when>
<xsl:otherwise>
<xsl:template match="entry[#key='name']"/>
</xsl:otherwise>
</xsl:choose>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="fileName">
<xsl:value-of select="base-uri()" />
</xsl:variable>
<xsl:template match="/">
<xsl:result-document href="{$fileName}.xml">
<xsl:apply-templates/>
</xsl:result-document>
</xsl:template>
</xsl:transform>
Could someone please help me with this to get the desired output xml.
Couldn't you do simply:
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="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="entry">
<entry key="cm:{#key}">
<xsl:apply-templates/>
</entry>
</xsl:template>
<xsl:template match="entry[#key='name'][not(string())]"/>
</xsl:stylesheet>

Related Links

How to stop <![CDATA[ to turn part of the code into comment, in Coda v.2 (the app)
How to process or describe a required property when generating a Maven project?
importxml not selecting element in span google sheets
Open docx content within Eclipse XML editor
Get user's authorities in the XML
Looking for Angular2 component for XML mapping
Structure of Eclipse .PSF file
Inkscape adding seemingly random amount of pixels to vertical coordinates
Is it a way to transform XML using XSLT Regex
How to do sum and remove duplicate using group by in xslt
Count of an XML child node's attribute values using Python 3.x
Using XML with XPath
Remove tag content from XML in linux console
Move xml element with xslt
Oozie email action escape tags
Need to arrange the topic elements properly using XSLT

Categories

HOME
phantom-dsl
fuelux
nvd3.js
object
numbers
fftw
shiro
error-handling
vxml
hpc
google-admin-sdk
data-visualization
mvc5
pkcs#11
performancepoint
sendgrid-api-v3
okhttp
owin
jxl
enterprise-architect
http-post
azure-powershell
internationalization
powershell-v5.0
job-scheduling
red5
html5-appcache
git-extensions
google-container-registry
pyramid
nim
boost-icl
topology
drive
microsoft-ocr
angular-routing
imageresizer
mat
fractions
autodesk-designautomation
event-flow
mobx
mockjax
left-join
glib
pop3
jwplayer6
searchkit
xcode7.3
filesystemwatcher
health-monitoring
android-softkeyboard
glm-math
oracle-service-bus
spring-integration-sftp
dbcontext
activeperl
video-conferencing
android-testing
gpg-signature
cvxpy
stripe-connect
stax
segment-io
greatest-n-per-group
observablecollection
nservicebus5
aspen
false-sharing
linklabel
cgi-bin
slash
dronekit-android
dnssec
cryptographichashfunction
filesplitting
post-commit-hook
paste
fpdi
vbe
modular
cocoalibspotify-2.0
unit-of-work
scringo
joomla-template
mkv
boost-signals
database-create
jquery-address
procedures
winmain
exponentiation
rmdir
umra
event-receiver
quotation-marks
socketserver
ie-compatibility-mode
aspmenu
hbm2java
asynchronous-wcf-call
fault-tolerance
flvplayback
online-storage
cardspace

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