xml


XSLT - add new node to given position


I have a xml like following xml,
<doc>
<footnote>
<p type="Foot">
<link ref="http://www.facebook.com"/>
<c>content</c>
<d>cnotent</d>
</p>
<p type="Foot">
<link ref="http://www.google.com"/>
<c>content</c>
<d>cnotent</d>
</p>
</footnote>
</doc>
My requirmets are,
1) add dynamic id to <p> node which has attribute type="Foot"
2) add new node named <newNode> inside <p> node
3) add dynamic id to <newNode>
so output sholud be
<doc>
<footnote>
<p id="ref-1" type="Foot">
<newNode type="direct" refId="foot-1"/>
<link ref="http://www.facebook.com"/>
<c>content</c>
<d>cntent</d>
</p>
<p id="ref-2" type="Foot">
<newNode type="direct" refId="foot-2"/>
<link ref="http://www.google.com"/>
<c>content</c>
<d>cotent</d>
</p>
</footnote>
</doc>
I wrote following xsl to do that
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- add new dynamic attribute to <p> -->
<xsl:template match="p[#type='Foot']">
<xsl:copy>
<xsl:attribute name="id">
<xsl:value-of select="'ref-'"/>
<xsl:number count="p[#type='Foot']" level="any"/>
</xsl:attribute>
<xsl:apply-templates select="#*|node()" />
<!-- add new node with dynamic attribute -->
<newNode type="direct">
<xsl:attribute name="refId">
<xsl:value-of select="'foot-'"/>
<xsl:number count="p[#type='Foot']" level="any"></xsl:number>
</xsl:attribute>
</newNode>
</xsl:copy>
</xsl:template>
My problem is it add new node ad last node inside <p> node (as show below) which I need to add as first node inside <p> node
<p id="ref-1" type="Foot">
<link ref="http://www.facebook.com"/>
<c>content</c>
<d>cntent</d>
<newNode type="direct" refId="foot-1"/>
</p>
How can I place as first node inside <p> node as shown below?
<p id="ref-1" type="Foot">
<newNode type="direct" refId="foot-1"/>
<link ref="http://www.facebook.com"/>
<c>content</c>
<d>cntent</d>
</p>
You need to make sure you copy the child elements after creating the new node:
<xsl:template match="p[#type='Foot']">
<xsl:copy>
<xsl:attribute name="id">
<xsl:value-of select="'ref-'"/>
<xsl:number count="p[#type='Foot']" level="any"/>
</xsl:attribute>
<xsl:apply-templates select="#*" />
<!-- add new node with dynamic attribute -->
<newNode type="direct">
<xsl:attribute name="refId">
<xsl:value-of select="'foot-'"/>
<xsl:number count="p[#type='Foot']" level="any"></xsl:number>
</xsl:attribute>
</newNode>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

Related Links

The definition of the report is invalid
Nested Parsing in XPath using Pig
What is iOS mobile config equivalent in android, Do we have a config utility
xml file changes its characters when copying from excel
how to create XML file using visual basic
How to get SOAP XML request / response documents in Spring WS?
Split XML file using XSLT 1.0 based on Number of Nodes as counter a value
XMLParser has problems reading UTF8 characters
Using XSLT to create DITA to XML output multiple same element comes under child element to a parent element
Getting “This XML file does not appear to have any style information associated with it” on basic convert
Angular2: Work correctly with XML REST response
how to show archived task in many2many tree view on another model in Odoo9?
how to add pdf file to xslt as html
How to copy child element n times in XML file using XSLT
Generating Flat file output in specific order using XSLT
Passing list of key-value elements to custom JMS Appender

Categories

HOME
polymer
object
static
grafana
visual-studio-emulator
iis-6
jax-ws
frontend
teechart
windows-ce
bitnami
delphi-xe5
database-connection
user-experience
roslyn
scriptella
zxing
comparator
syntax-highlighting
jackson-modules
underscore.js
datastax-enterprise-graph
cppreference
scatter3d
glib
dsx-desktop
contacts
openwhisk
hibernate-search
servicestack-text
xajax
raft
zoomify
timing
cin
numberpicker
tasm
sharp-snmp
avcapturedevice
nanoc
dtsearch
datamaps
colorbar
mathcad
django-1.10
filenet
gets
cronexpression
custom-lists
redbeard-ios
dotnetnuke-7
php-socket
dbamp
kiosk
maybe
java-money
qt5.2
poppler
.bash-profile
batik
kubuntu
j-security-check
grouping-sets
nodelist
contrast
sql-scripts
spy++
appserver
tweenlite
jama
cuba
mutual-authentication
syndicationfeed
spring-3
gtm-oauth2
inappsettingskit
tlf
actiondispatch
robotlegs
pdfviewer
extreme-programming
pstack
dajaxice
cuda-gdb
appjs
apache-whirr
fitbounds
jquery-address
box2d-iphone
convention
umra
resharper-6.0
tabpanel
lobo-cobra
alsb
scrubyt
entitykey

Resources

Encrypt Message