Lisa
Lisa

Reputation: 77

Remove attribute value before #

I have many xml files that have an element like this:

<link refID="hdb-tbl-map.dita#topic_x33_ttl_4q">

or

<link refID="#topic_xwd_fbt_4q" format="dita">

I want to only have the value after # to be present in the attribute value.

So the resulting xml will have the elements like this without the format attribute.

<link refID="topic_x33_ttl_4q">

and

<link refID="topic_xwd_fbt_4q">

I need to change only this element and nothing else in the xml.

Upvotes: 0

Views: 57

Answers (3)

uL1
uL1

Reputation: 2167

Therefore you are asking for a XSLT solution (as tagged), try this one:

XSLT 1.0 + 2.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">

    <xsl:template match="link/@refID">
        <xsl:attribute name="{name()}">
            <xsl:value-of select="substring-after(.,'#')"/>
        </xsl:attribute>       
    </xsl:template>

    <xsl:template match="link/@format"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Result for example

<link refID="topic_x33_ttl_41"/>

Explanation

First template matches on attribute @refID of element link. Recreates the attribute with same name {name()}.

Second template matches attribute @format of element link and does nothing, so it will be removed.

Last template is a identity copy template. Copy from source to target 1:1.

Upvotes: 1

Maettel
Maettel

Reputation: 46

Try this one: (I'm assuming you are in a java surrounding and parsing these xml-strings?)

str = str.replaceAll("\".*#(.*)\"", "$1");

What it does?

Searching the character ", then some chars, then #, then get the next part until another " is given.

Upvotes: 0

Petar Petrov
Petar Petrov

Reputation: 596

If you are using eclipse :

Ctrl + H -> File search` -> Containing text : #   File name patterns : *.xml -> Replace 

In the new windows on input 'with' put ""

Upvotes: 0

Related Questions