Unwrap elements with XSLT - xslt-1.0

I have large and complex documents which contain the following at various places in the document the following fragments
<a>foo<b>bar</b><a>
<a>foo<b>bar</b><b>hello</b><a>
which I want to transform to
<b>foobar<b>
<b>foobar<b><b>hello</b>

Use the following two template matchers:
<xsl:template match="a">
<xsl:apply-templates select="b"/>
</xsl:template>
<xsl:template match="b">
<xsl:copy>
<xsl:if test="position() = 1">
<xsl:value-of select="parent::a/text()"/>
</xsl:if>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>

Related

Find last translate

I have the following xml:
<item>value 1,value 2,value3,</item>
I would now like to achieve the following via an output via xslt:
<item>value 1,value 2,value3</item>
So remove the last comma. Can you help me?
Use this template:
<xsl:template match="item">
<xsl:copy>
<xsl:choose>
<xsl:when test="substring(.,string-length(.))=','">
<xsl:value-of select="substring(.,1,string-length(.)-1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
It will only remove the last char when it is a comma.

xslt replace value of multiple elements

I need to replace values of multiple elements of an input xml. The input file has around 300 elements of which I need to replace around 100 elements. I have used identity template before, but I think it would require me to write 100 different templates each for replacing a single element value. I am not very good at xslts, so, am I thinking it right, or is there a better an elegant approach? Please advice.
Edit
Here is the Link of sample input xml.
The output will have almost the same structure, but different values for some of the elements.
Well, something I did similar before before so I am happy to share ... modified to your example BUT does not do the value mapping, it does name mapping.
First create a simple mapping file like this (NOTE: you would need the namespaces to do this right
<env:map xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<elem old="CardCode" new="CodeCardNEW"/>
<elem old="CardName" new="IAMNew"/>
</env:map>
Then one template will do you with a lookup. There are likely better ways to do the mapping, I am just for-each looping over them all ... a key would be better. But this gets you there.
Output from your file with above:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Body>
<GetByKeyResponse>
<BOM>
<BO>
<AdmInfo>
<Object>oBusinessPartners</Object>
</AdmInfo>
<BusinessPartners>
<row>
<CodeCardNEW>CR43WEB</CodeCardNEW>
<IAMNew>Zack Burns</IAMNew>
<CardType>cCustomer</CardType>
...
And here's the XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:env="http://www.w3.org/2003/05/soap-envelope"
version="1.0">
<xsl:param name="map" select="document('map.xml')/env:map"></xsl:param>
<xsl:template match="text()" priority="1">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:variable name="newname">
<xsl:call-template name="namesub">
<xsl:with-param name="name" select="name()"/>
</xsl:call-template>
</xsl:variable>
<xsl:element name="{$newname}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template name="namesub">
<xsl:param name="name"/>
<xsl:variable name="newname">
<xsl:for-each select="$map/elem">
<xsl:choose>
<xsl:when test="#old = $name">
<xsl:value-of select="#new"/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="string-length($newname) > 0">
<xsl:value-of select="$newname"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
You should be able to adapt this as long as you have a match on name ... if the name is the same in different hierarchies of the XML, you would need more work.

XSLT1 error when get first occurence of a tag

This question (and my problem) is similar to XSLT1 Get the first occurence of a specific tag... But I have an "Undefined variable" error.
I have a XML with refpos attribute, that can be make by this first XSLT,
<xsl:template match="p[#class='ref']">
<xsl:copy>
<xsl:attribute name="refpos">
<xsl:value-of select="position()"/>
</xsl:attribute>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()" name="idt">
<xsl:copy><xsl:apply-templates select="#*|node()"/></xsl:copy>
</xsl:template>
Then, I a main (second) XSLT I have,
<xsl:variable name="firstRef">
<xsl:value-of select="(//p[#class='ref'])[1]/#refpos"/>
</xsl:variable>
<xsl:template match="p[#refpos=$firstRef]">
<title><xsl:apply-templates /></title>
</xsl:template>
<xsl:template match="#*|node()" name="idt">
<xsl:copy><xsl:apply-templates select="#*|node()"/></xsl:copy>
</xsl:template>
But here this XSLT not works!!
PS: I also believe that XSLT1 allow us to do everything in one step.
XSLT 1.0 does not allow variable references in template match expressions (XSLT 2.0 does). You'll have to move the check from the predicate inside the template:
<xsl:template match="p">
<xsl:choose>
<xsl:when test="#refpos=$firstRef">
<title><xsl:apply-templates /></title>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="idt" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Sanitizing DB inputs with XSLT

I've been looking for a method to strip my XML content of apostrophes (') since my DBMS is complaining of receiving those.
I need
<name> Jim O'Connor</name>
to become:
<name> Jim O''Connor</name>
By looking at the example described here, that is supposed to replace ' with '', I constructed the following script:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template name="sqlApostrophe">
<xsl:param name="string" />
<xsl:variable name="apostrophe">'</xsl:variable>
<xsl:choose>
<xsl:when test="contains($string,$apostrophe)">
<xsl:value-of select="concat(substring-before($string,$apostrophe), $apostrophe,$apostrophe)"
disable-output-escaping="yes" />
<xsl:call-template name="sqlApostrophe">
<xsl:with-param name="string"
select="substring-after($string,$apostrophe)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"
disable-output-escaping="yes" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="sqlApostrophe">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
UPDATE: it works fine
Thanks for your help
The main problem is in your last template. As dacracot points out, xsl:apply-templates does not take a name attribute. To call a named template, you'd use xsl:call-template.
If you want to apply your SQL escaping to all text nodes, you could try replacing your last template with something like this:
<xsl:template match="text()">
<xsl:call-template name="sqlApostrophe">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</xsl:template>
Also, why disable-output-escaping? If the text contains special characters (<, >, &), you'll get malformed XML as the output.
You have a couple of issues syntactically...
You can't have a name attribute on an apply-templates tag.
Your xpath, "node()|#*", is ambiguous.
Have you run this through a debugger? I would suggest Oxygen.

How to Generate XML from Database

I am fetching data from two tables CARRIER_IFTA ,IFTA_NAME.
My Select Query is like below..
SELECT t1.IFTA_LICENSE_NUMBER,t1.IFTA_BASE_STATE,t2.NAME_TYPE,t2.NAME
from CARRIER_IFTA t1 inner join IFTA_NAME t2
on t1.IFTA_LICENSE_NUMBER=t2.IFTA_LICENSE_NUMBER
My Data is coming in this way...
IFTA_LICENSE_NUMBER IFTA_BASE_STATE NAME_TYPE NAME
--------------------------------------------------------
630908333 US LG XYZ
630908333 US MG PQR
730908344 US LG ABC
Now using XSLT I want to generate XML like this
<T0019>
<IFTA_ACCOUNT>
<IFTA_LICENSE_NUMBER>630908333</IFTA_LICENSE_NUMBER>
<IFTA_BASE_STATE>US</IFTA_BASE_STATE>
<IFTA_NAME>
<NAME_TYPE>LG<NAME_TYPE>
<NAME>XYZ</NAME>
</IFTA_NAME>
<IFTA_NAME>
<NAME_TYPE>MG<NAME_TYPE>
<NAME>PQR</NAME>
<IFTA_NAME>
</IFTA_ACCOUNT>
<IFTA_ACCOUNT>
<IFTA_LICENSE_NUMBER>730908344</IFTA_LICENSE_NUMBER>
<IFTA_BASE_STATE>US</IFTA_BASE_STATE>
<IFTA_NAME>
<NAME_TYPE>LG<NAME_TYPE>
<NAME>ABC</NAME>
</IFTA_NAME>
</IFTA_ACCOUNT>
</T0019>
i have used below xslt but it is not giveng me desire result ...
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/ROWSET">
<xsl:element name="T0019">
<xsl:apply-templates select="IFTAACCOUNT"/>
</xsl:element>
</xsl:template>
<xsl:template match="IFTAACCOUNT">
<xsl:element name="IFTAACCOUNT">
<xsl:apply-templates select="IFTA_CARRIER_ID_NUMBER"/>
</xsl:element>
</xsl:template>
<xsl:template match="IFTA_LICENSE_NUMBER">
<xsl:element name="IFTA_LICENSE_NUMBER">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="IFTA_BASE_STATE">
<xsl:element name="IFTA_BASE_STATE">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="IRP_NAME">
<IRP_NAME>
<xsl:apply-templates select="NAME"/>
<xsl:apply-templates select="NAME_TYPE"/>
</IRP_NAME>
</xsl:template>
<xsl:template match="NAME">
<xsl:element name="NAME">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="NAME_TYPE">
<xsl:element name="NAME_TYPE">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
but it is not giving desire result ...
Please help me ...
Thanks in Advance...
Most RDBMS provides builtin tools to return straight XML without the need for XSL. Which one are you using? Consult its documentation. If it is for example MySQL, then you need the --xml option.