• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

XSLT Issue

Hi All,

I need to write a XSLT to remove a particular character from the XML tags for ex:

<ABCD-EFGHI> I want to remove the dash (-) in this case.

Please let me know how I can.

Thanks & Regards
Pradeep
0
Pradeep0308
Asked:
Pradeep0308
  • 6
  • 4
2 Solutions
 
selva_konguCommented:
<?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="text" indent="yes"/>

    <xsl:template match="text()">
      <xsl:value-of select="translate(., '&lt;/&gt;-', '')"/>
    </xsl:template>
</xsl:stylesheet>


the above xslt will remove the - with space
0
 
Pradeep0308Author Commented:
Hi,

This is removing the whole XML tag.

Regards
Pradeep
0
 
Geert BormansCommented:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="*">
        <xsl:element name="{translate(name(), '-', '')}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="text() | comment() | processing-instruction()">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Open in new window


will remove the '-' from the element names
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Pradeep0308Author Commented:
Hi,

I had used the input as

<Test-1>This is a test document </Test-1>

and the Output I am getting is

This is a test document

I need the output as XML document defined below:

</Test1> This is a test document </Test1>
0
 
Geert BormansCommented:
Try my solution, It does exactly that, just tested it with your test string
0
 
Pradeep0308Author Commented:
Hi,

Thanks it works. What if I want to remove this dash only from a specific XML element name and not all the XML elements.

Regards
Pradeep
0
 
Geert BormansCommented:
the suggestion of selva_kongu only deals with text nodes and as such will have the default XSLT behaviour of stripping the element tags off

the translate function works as follows
each character from the second argument found in the first argument will be replaced with the character at the same location in the third argument, if there is no character at that location in the third argument, it will be dropped

<xsl:element name="{translate(name(), '-', '')}">
creates an element
the name is a string operation on the original element name()
'-' is at the first location in the 2nd argument
third argument is empty string, so no character at the first location.
in name() each '-' will be replaced with nothing

in your example changing my code to this
<xsl:element name="{translate(name(), 'eTs-', 'obo')}">
will make the result
<boot1>This is a test document </boot1>
check that out :-)
0
 
Geert BormansCommented:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="Test-1">
        <xsl:element name="{translate(name(), '-', '')}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text() | comment() | processing-instruction()">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
Geert BormansCommented:
will work for <Test-1> only
now you can be smart in the match attribute of the first template to cover more than one element
0
 
Pradeep0308Author Commented:
Hi Gertone,

Worked like a charm. Thanks a ton.

Regards
Pradeep
0
 
Geert BormansCommented:
welcome
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now