How to check element existence and select substring of element value
I have an XML file :
<?xml version="1.0" encoding="windows-1250"?>
<CONTACTS>
<CONTACT>
<FirstName>AfgZohal</FirstName>
<LastName>Zohal Afg</LastName>
</CONTACT>
<CONTACT>
<FirstName>Arun_niit</FirstName>
<LastName>Arun_niit</LastName>
<EMail>nura_ice@yahoo.co.in</EMail>
</CONTACT>
<CONTACT>
<FirstName>Bống MũnHải</FirstName>
<LastName>Hải Anh Bống Mũn</LastName>
<URL>http://www.facebook.com/profile.php?id=100000689849077</URL>
</CONTACT>
</CONTACTS>
I want to add an element ID before FirstName in my xml file;I would like to extract ID from URL tag if URL is available or I want to extract the first six letters from Email address to put it in the ID(unique). Because in some contacts, there is no URL. I using XSLT for this. In my XSl file i tried in this way
<ID>
<xsl:value-of select="CONTACT/URL[//http='@id']"/>
</ID>
but it's not working, this is my XSL file:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CONTACT">
<xsl:copy>
<ID>
<xsl:value-of select="CONTACT/URL[//http='@id']"/>
</ID>
<xsl:copy-of select="FirstName|LastName|URL"/>
<EMAILS>
<xsl:apply-templates select="EMail"/>
</EMAILS>
</xsl:copy>
</xsl:template>
<xsl:template match="EMail">
<EMail>
<Type><xsl:value-of select="substring-before(
substring-after(.,'@'),
'.')"/>
</Type>
<Value><xsl:value-of select="."/></Value>
</EMail>
</xsl:template>
</xsl:stylesheet>
This is my output xml file:
<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>
<CONTACT>
<ID/>
<FirstName>AfgZohal</FirstName>
<LastName>Zohal Afg</LastName>
<EMAILS/>
</CONTACT>
<CONTACT>
<ID/>
<FirstName>Arun_niit</FirstName>
<LastName>Arun_niit</LastName>
<EMAILS>
<EMail>
<Type>yahoo</Type>
<Value>nura_ice@yahoo.co.in</Value>
<开发者_Go百科;/EMail>
</EMAILS>
</CONTACT>
<CONTACT>
<ID/>
<FirstName>Bống MũnHải</FirstName>
<LastName>Hải Anh Bống Mũn</LastName>
<URL>http://www.facebook.com/profile.php?id=100000689849077</URL>
<EMAILS/>
</CONTACT>
<CONTACT>
</CONTACTS>
This is part of my yesterday problem Novice transformation using apply-templates and string manipulation in the child node; since it's a different issue i'm raising a different question.
It seems that you want to select the 'id' part of the url string inside the URL
element. You should select the substring after ?id=
:
<ID>
<xsl:value-of select="substring-after(URL,'?id=')"/>
</ID>
Moreover, in the template you are in the context of CONTACT
, ergo to select a child of it you need just to specify the name of the element. Example:
<xsl:template match="CONTACT">
<xsl:value-of select="URL"/>
</xsl:template>
Will return the value of URL
while:
<xsl:template match="CONTACT">
<xsl:value-of select="CONTACT/URL"/>
</xsl:template>
won't return anything, not having CONTACT
any child of type CONTACT/URL
.
Bonus answer to comment question:
I would like to extract ID from URL tag if URL is available or I want to extract the first six letters from Email address to put it in the ID(unique) (...) If we have one/multiple email address then we can select any one of the first six letters from the email address. I thing the contacts must have at least one email address if there is no URL...
<ID>
<xsl:choose>
<xsl:when test="URL">
<xsl:value-of select="substring-after(URL,'?id=')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(EMail[1],'@')"/>
</xsl:otherwise>
</xsl:choose>
</ID>
精彩评论