my xml: The input xml to be transformed
<content>
<conditionalText name="Masked_Account_Number">
<text>
Hi 2<dynamicVariable name="asked_Account"/> is needed.
</text>
</conditionalText>
<dynamicInclude name="xyz" />
</content>
So I have to use above xml,identify all the tags containing 'name' as attribute,extract value of it and create tag 'name' and under it , itshould have 'a' tag
the format is as follows :
transformed output for all tags containing 'name' as their attribute in input xml should be as:
<name>
<a href="#" id="dynamicvariable"
name="value of name attribute"
xmlns="http://www.w3.org/1999/xhtml">[value of name attribute]
</a>
</name>
and only for input xml file's 'dynamicVariable' Tag it should only create 'a' tag as
transformed output for 'dynamicVariable' tag of input xml should look like:
<a href="#" id="dynamicvariable"
name="value of name attribute"
xmlns="http://www.w3.org/1999/xhtml">[value of name attribute]
</a>
so my output xml after transformation should look like
<content1>
<conditionalText>
<text>
Hi 2
<a href="#" id="dynamicvariable"
name="asked_Account"
xmlns="http://www.w3.org/1999/xhtml">[asked_Account]
</a>is needed
</text>
<name>
<a href="#" id="dynamicvariable"
name="Masked_Account_Number"
xmlns="http://www.w3.org/1999/xhtml">[Masked_Account_Number]
</a>
</name>
</conditionalText>
<dynamicInclude>
<name>
<a href="#" id="dynamicvariable" name="xyz"
xmlns="http://www.w3.org/1999/xhtml">[xyz]
</a>
</name>
</dynamicIncude>
</content1>
XSLT tried is given below:
I have took two templates.First template will identify all the tags containing 'name' as their attribute and extract value of it and creates in format as
<name>
<a href="#" id="dynamicvariable"
name="value of name attribute"
xmlns="http://www.w3.org/1999/xhtml">[value of name attribute]
</a>
</name>
And the second template will override the first template and it will identify only tag with name 'dynamicVariable' and extracts its 'name' attribute value and creates tag with format as
<a
href="#" id="dynamicvariable"
name="value of name attribute"
xmlns="http://www.w3.org/1999/xhtml">[value of name attribute]
</a>
so my final xslt is as below:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="content/*">
<content1>
<xsl:apply-templates />
</content1>
</xsl:template>
<xsl:template match= "*/#name" >
<name>
<a href ='#' id="dynamicvariable" xmlns="http://www.w3.org/1999/xhtml" >
<xsl:copy-of select="#*"/>
[<xsl:value-of select="#name"/>]
</a>
</name>
<xsl:for-each select="child::*">
<xsl:if test="name()='text'">
<text>
<xsl:apply-templates />
</text>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match= "*[name()='dynamicVariable']" >
<a href ='#' id="dynamicvariable" xmlns="http://www.w3.org/1999/xhtml" >
<xsl:copy-of select="#*"/>
[<xsl:value-of select="#name"/>]
</a>
</xsl:template>
</xsl:stylesheet>
but didnt get the required transformed xml .
Can anyone help me out .
and the transformed xml file having conditionalText element should have then as subchilds in the same
sequence as specified in the transformed xml.
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*[not(name()='name')]"/>
<xsl:apply-templates select="#name"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/#name">
<name>
<a href="#" id="dynamicvariable"
name="{.}"
xmlns="http://www.w3.org/1999/xhtml">[<xsl:value-of select="."/>]
</a>
</name>
</xsl:template>
<xsl:template match="dynamicVariable">
<a href="#" id="dynamicvariable"
name="{#name}"
xmlns="http://www.w3.org/1999/xhtml">[<xsl:value-of select="#name"/>]
</a>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<content>
<conditionalText name="Masked_Account_Number">
<text>
Hi 2<dynamicVariable name="asked_Account"/> is needed.
</text>
</conditionalText>
<dynamicInclude name="xyz" />
</content>
produces the wanted (the renaming of content is left as an exercise to the reader), correct result:
<content>
<conditionalText>
<text>
Hi 2<a xmlns="http://www.w3.org/1999/xhtml" href="#" id="dynamicvariable" name="asked_Account">[asked_Account]
</a> is needed.
</text>
<name>
<a xmlns="http://www.w3.org/1999/xhtml" href="#" id="dynamicvariable" name="Masked_Account_Number">[Masked_Account_Number]
</a>
</name>
</conditionalText>
<dynamicInclude>
<name>
<a xmlns="http://www.w3.org/1999/xhtml" href="#" id="dynamicvariable" name="xyz">[xyz]
</a>
</name>
</dynamicInclude>
</content>
Related
Given the following XML document
<root>
<a pos="0" total="2"/>
<a pos="1" total="2"/>
<a pos="0" total="3"/>
<a pos="1" total="3"/>
<a pos="2" total="3"/>
<a pos="0" total="4"/>
<a pos="1" total="4"/>
<a pos="2" total="4"/>
<a pos="3" total="4"/>
</root>
I need to translate it to
<root>
<group>
<a pos="0" total="2"/>
<a pos="1" total="2"/>
</group>
<group>
<a pos="0" total="3"/>
<a pos="1" total="3"/>
<a pos="2" total="3"/>
</group>
<group>
<a pos="0" total="4"/>
<a pos="1" total="4"/>
<a pos="2" total="4"/>
<a pos="3" total="4"/>
</group>
</root>
using an XSLT 1.0 stylesheet.
That is, each <a> element with the #pos attribute of 0 in the document
implicitly starts a group consisting of it and the #total-1 following <a> elements. To explain this in other words, #pos denotes a 0-based index (position) of the element in the group of #total adjacent elements.
I have come up with the following stylesheet, which works:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:apply-templates select="root" />
</xsl:template>
<xsl:template match="root">
<xsl:apply-templates select="a[#pos=0]" mode="leader"/>
</xsl:template>
<xsl:template match="a" mode="leader">
<group>
<xsl:apply-templates select="." />
<xsl:apply-templates select="following-sibling::a[position() <= current()/#total - 1]" />
</group>
</xsl:template>
<xsl:template match="a">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
The problem I have with my solution is that it makes those a[#pos=0] elements "special": to further process each <a> element in a prospective group, I have to separately apply the appropriate template first to the "group leader" element and then to the rest of the elements in the group.
In other words I would very much like to have something like (incorrect)
<xsl:template match="a" mode="leader">
<group>
<xsl:apply-templates select=". and following-sibling::a[position() <= current()/#total - 1]" />
</group>
</xsl:template>
which would apply my <xsl:template match="a"> template to all the elements in the group in one go. (To rephrase what I've attempted to spell in the select expression: "select the context element and its following sibling elements matching …".)
Is there a way to have what I want with XSLT 1.0 without resorting to hacks like variables and exslt:node-set()? May be there's a better way to do such grouping based on element counts than the one I have come up with (which inherently makes the first element in each group special)?
I admit the question's title is rather weak but I failed to come up with a succint one which correctly reflect the essense of my question.
You could do:
<xsl:apply-templates select=". | following-sibling::a[position() <= current()/#total - 1]" />
P.S. Using variables or the node-set() function does not qualify as a "hack".
Is it possible to apply template on copy of element?
Let say I have this XML:
<root>
<a name="a1">
A1
<b name="b1">B1</b>
</a>
<b name="b2">B2</b>
<a name="a2">
<b name="b3">B3</b>
</a>
<b name="b4">B4</b>
<a name="a3">
<b name="b5">B5</b>
</a>
</root>
and want to turn it into this XML:
<root>
<a name="a1">
A1
<b name="b1">B1</b>
<b name="b2">B2</b>
</a>
<a name="a2">
<b name="b3">B3</b>
<b name="b4">B4</b>
</a>
<a name="a3">
<b name="b5">B5</b>
</a>
</root>
and than apply this templates on new structure:
<xsl:template match="/">
1<xsl:apply-templates/>2
</xsl:template>
<xsl:template match="a">
3<xsl:apply-templates select="b[2]"/>4
</xsl:template>
<xsl:template match="b[not(position()=2)]">
5<xsl:apply-templates/>6
</xsl:template>
<xsl:template match="b[2]">
7<xsl:apply-templates/>8
</xsl:template>
so output should be:
1
3
A1
5B16
7B28
4
3
5B36
7B48
4
3
5B56
4
2
Important thing is I want to use normal templates on new structure
So far I have this but it doesn't work (doesn't recognize new structure)
<xsl:template match="/">
1
<xsl:apply-templates select="#*|node()"/>
2
</xsl:template>
<xsl:template match="a">
3
<xsl:for-each select="following-sibling::b[1]">
<xsl:apply-templates select="." mode="aaa"/>
</xsl:for-each>
4
</xsl:template>
<xsl:template match="b"/>
<xsl:template match="b[not(position()=2)]" mode="aaa">
5<xsl:apply-templates/>6
</xsl:template>
<xsl:template match="b[2]" mode="aaa">
7<xsl:apply-templates/>8
</xsl:template>
I've the below Sample XMLs.
XML1
<root num="1">
<abc></abc>
<cde></cde>
<def></def>
</root>
XML2
<root num="2">
<xyz></xyz>
<cft></cft>
<vft></vft>
</root>
XML3
<root num="3">
<dfg></dfg>
<mnb></mnb>
<gft></gft>
<root>
And i have 3 different XSLTs, each corresponding to XML.
I want to achieve the below.
Make a single XSLT and call the template based on the root number. something like the below.
<xsl:if test="root[#num="1"]>
<!--Call the template matching root 1-->
</xsl:if>
<xsl:if test="root[#num="2"]>
<!--Call the template matching root 2-->
</xsl:if>
<xsl:if test="root[#num="3"]>
<!--Call the template matching root 3-->
</xsl:if>
I just want to put all the XSLTs in a single XSLT, please let me know how can i do this.
Thanks
You can use element.
The element contains rules to apply when a specified node is matched.
The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).
Note: is a top-level element.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
This was taken from http://www.w3schools.com/xsl/el_template.asp
My scenario is that we have to check for a div tag in the cdata of a body element. If div is present we have to insert the the text from node2 into the div tag.
This is my input xml:
<?xml version="1.0" encoding="utf-8"?>
<root>
<node1>abc</node1>
<node2> needs to replace inside cdata div</node2>
<body> <![CDATA[
<p>some text some textabcabcabcabc</p>
<div class="marginBottom_4px">
</div>
<p>some text some textabcabc</P>
]]>
</body>
</root>
The out put xml would be:
<?xml version="1.0" encoding="utf-8"?>
<div class="marginBottom_10px">
abc
</div>
<div class="marginBottom_5px">
<p>some text some textabcabcabcabc</p>
<div class="marginBottom_4px">
needs to replace inside cdata div
</div>
<p>some text some textabcabc</P>
</div>
My transform is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:value-of disable-output-escaping="yes" select ="$firstnode"/>
<xsl:text disable-output-escaping="yes"><![CDATA[ <div class="marginBottom_10px">
]]>
</xsl:text>
<xsl:value-of disable-output-escaping ="yes" select="root/body"/>
<xsl:text disable-output-escaping="yes"><![CDATA[
</div>
]]>
</xsl:text>
</xsl:template>
<xsl:variable name="firstnode">
<xsl:text disable-output-escaping="yes"><![CDATA[
<div class="marginBottom_10px">
]]>
</xsl:text>
<xsl:value-of disable-output-escaping ="yes" select="root/node1"/>
<xsl:text disable-output-escaping="yes"><![CDATA[
</div>
]]>
</xsl:text>
</xsl:variable>
</xsl:stylesheet>
I am able to produce the out put. but my xml is very complex like below:
<?xml version="1.0" encoding="utf-8" ?>
<ComplexXML>
<environment>
couple of nodes..
</environment>
<document>
nodes
</document>
<element cd="dsjdhfjk" input="abc.xml" mode="" >
<cd position="1">
<attributes>
<type>dummy text</type>
<title>dummy text</title>
</attributes>
<content>
<node2>
<![CDATA[
needs to replace inside cdata div
]]>
</node2>
<body>
<![CDATA[
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
specimen book </p>
<div class="marginBottom_4px">
</div>
<p>Lorem Ipsum is simply dummy text of her including versions of Lorem Ipsum. </p>
]]>
</body>
<abt >
<![CDATA[
text from abt node
]]>
</abt>
</content>
</cd>
</element>
</ComplexXML>
In the above xml I have to check for the abt node.If data is there in abt node the out should be like below:
<?xml version="1.0" encoding="UTF-8"?>
<div>
text from abt node
<div class="marginBottom_5px">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
specimen book </p>
**<div class="marginBottom_4px">
</div>** I need to remove this div tag and place the node2 content here.
<p>Lorem Ipsum is simply dummy text of her including versions of Lorem Ipsum. </p>
</div>
</div>
Sorry to bother you..I am very new to xslt..I am in learning stage only..Can you please guide me..
The following XSLT 1.0 stylesheet produces what I believe the desired output is, based upon the example output and comments. It relies upon the text in the CDATA of the input document being well-formed, and leverages disable-output-escaping:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node1">
<div class="marginBottom_10px">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="node2" />
<xsl:template match="body">
<div class="marginBottom_5px">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="body/text()[contains(.,'<div') and contains(.,'</div>')]">
<xsl:value-of
disable-output-escaping="yes"
select="substring-before(.,'</div')" />
<xsl:value-of select="../../node2"/>
<xsl:value-of
disable-output-escaping="yes"
select="substring-after(.,substring-before(.,'</div'))" />
</xsl:template>
</xsl:stylesheet>
When applied against the example input, produces:
<?xml version="1.0" encoding="UTF-8"?>
<div class="marginBottom_10px">abc</div>
<div class="marginBottom_5px">
<p>some text some textabcabcabcabc</p>
<div class="marginBottom_4px">
needs to replace inside cdata div</div>
<p>some text some textabcabc</P>
</div>
Note: the output is not well-formed. There is no document element. You would likely want to create a template for the <root> to create a <div> or other containing element.
This version handles the other input format and generates what I believe the desired output is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<!--rely on built-in templates,
which apply-templates to child nodes of elements
and get value-of for text() -->
<xsl:template match="content">
<xsl:choose>
<!-- if <abt> has a value, do the following -->
<xsl:when test="normalize-space(abt)">
<div>
<!-- apply templates to <abt>,
built-in template will copy text to output-->
<xsl:apply-templates select="abt"/>
<!-- apply templates to <body>, template defined below will handle it -->
<xsl:apply-templates select="body"/>
</div>
</xsl:when>
<xsl:otherwise>
<!--process child nodes -->
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="node1">
<div class="marginBottom_10px">
<xsl:apply-templates/>
</div>
</xsl:template>
<!--empty template ensures that no content produced when templates applied to <node2>-->
<xsl:template match="node2" />
<xsl:template match="body">
<div class="marginBottom_5px">
<xsl:apply-templates/>
</div>
</xsl:template>
<!--Template for handling body/text() when <abt> does not have a value-->
<xsl:template match="*[not(normalize-space(abt))]/body/text()[contains(.,'<div') and contains(.,'</div>')]">
<!--get the value of content preceding "</div"-->
<xsl:value-of
disable-output-escaping="yes"
select="substring-before(.,'</div')" />
<!--get the value of <node2> -->
<xsl:value-of select="../../node2"/>
<!--get the value of content starting at "</div" -->
<xsl:value-of
disable-output-escaping="yes"
select="substring-after(.,substring-before(.,'</div'))" />
</xsl:template>
<!--Template for handling body/text() when <abt> does have a value -->
<xsl:template match="*[normalize-space(abt)]/body/text()[contains(.,'<div') and contains(.,'</div>')]">
<!--get the value preceding "<div" -->
<xsl:value-of
disable-output-escaping="yes"
select="substring-before(.,'>div')" />
<xsl:value-of select="../../node2"/>
<!--get the value following "</div>" -->
<xsl:value-of
disable-output-escaping="yes"
select="substring-after(.,'</div>')" />
</xsl:template>
</xsl:stylesheet>
Can any one please suggest how to sort an XML by attribute names using XSLT?
For example: my XML is as below
<?xml version="1.0" encoding="UTF-8"?>
<root>
<!-- test 1 -->
<test g="r">
<a g="c" d="e">one</a>
<!-- a k z d b -->
<a k="z" d="b">two</a>
<a s="h" d="5">three</a>
<!-- a a b d 4 -->
<a a="b" d="4">four</a>
<a b="q" d="3">five</a>
<a s="a" d="8">3three</a>
<a x="i" d="2">six</a>
<!-- six 2 a f h i 2 -->
<a f="h" i="2">six</a>
<a l="t" d="1">seven</a>
</test>
<!-- test 2 -->
<test t="b">
<!-- six 2 a z i d 2 -->
<a z="i" d="2">six</a>
<a r="z" d="b">two</a>
<a a="c" d="e">one</a>
<a u="h" d="5">three</a>
<!-- four -->
<a c="b" d="4">four</a>
<a h="q" d="3">five</a>
<a p="t" d="1">seven</a>
</test>
</root>
expected output should be:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<!-- test 1 -->
<test g="r">
<!-- a a b d 4 -->
<a a="b" d="4">four</a>
<a b="q" d="3">five</a>
<a g="c" d="e">one</a>
<!-- six 2 a f h i 2 -->
<a f="h" i="2">six</a>
<!-- a k z d b -->
<a k="z" d="b">two</a>
<a l="t" d="1">seven</a>
<a s="a" d="8">3three</a>
<a s="h" d="5">three</a>
<a x="i" d="2">six</a>
</test>
<!-- test 2 -->
<test t="b">
<a a="c" d="e">one</a>
<!-- four -->
<a c="b" d="4">four</a>
<a h="q" d="3">five</a>
<a p="t" d="1">seven</a>
<a r="z" d="b">two</a>
<a u="h" d="5">three</a>
<!-- six 2 a z i d 2 -->
<a z="i" d="2">six</a>
</test>
</root>
I suspect you might be wanting it to sort on the name of the first attribute. That can't be done, because the order of attributes has no significance in XML, and you can't predict which attribute #*[1] will select.
<xsl:template match="test">
<xsl:apply-templates>
<xsl:sort select="#d"/>
</xsl:apply-templates>
</xsl:template>
will get you the elements under test sorted by attribute d.
You can do multiple sort-levels by adding another xsl:sort.
More about sorting here: http://www.w3.org/TR/xslt#sorting
I found the solution myself.. Below is the line of code to be used for sorting by attribute names.
<xsl:sort select="local-name(#*)"/>
Guys, thanks for all your efforts.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="#*">
<xsl:sort select="name()"/>
</xsl:apply-templates>
<xsl:apply-templates>
<xsl:sort select="name()"/>
<xsl:sort select= "name(#*)"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="#*">
<xsl:copy />
</xsl:template>
</xsl:stylesheet>