XSLT1.0: Replace subsring value of occurence of attribute in XML

I have XML file (ISO 19115 metadata XML file) and I want to change part of value of codelist attribute in whole XML. URL for whole XML file https://rpi.gov.sk/rpi_csw/service.svc/get?request=getrecordbyid&service=csw&version=2.0.2&outputSchema=http://www.isotc211.org/2005/gmd&resulttype=results&elementsetname=full&id=https://data.gov.sk/set/rpi/gmd/00626031/170123074833

I would like to replace part of codelist value for all occurences:

Example: INPUT:

 <gmd:hierarchyLevel>     <gmd:MD_ScopeCode codeList="https://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml#MD_ScopeCode" codeListValue="service">service</gmd:MD_ScopeCode> </gmd:hierarchyLevel> 

Expected OUTPUT:

<gmd:hierarchyLevel>     <gmd:MD_ScopeCode codeList="http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#MD_ScopeCode" codeListValue="service">service</gmd:MD_ScopeCode> </gmd:hierarchyLevel> 

I tried this:

<xsl:param name="findText" select="'https://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml'" /> <xsl:param name="replaceText" select="'http://standards.iso.org/iso/19139/resources/gmxCodelists.xml'" />  <xsl:template match="@*">     <xsl:call-template name="string-replace-all">         <xsl:with-param name="text" select="." />         <xsl:with-param name="replace" select="$findText" />         <xsl:with-param name="by" select="$replaceText" />     </xsl:call-template> </xsl:template>  <xsl:template name="string-replace-all">     <xsl:param name="text" />     <xsl:param name="replace" />     <xsl:param name="by" />     <xsl:choose>         <xsl:when test="contains($text, $replace)">             <xsl:value-of select="substring-before($text,$replace)" />             <xsl:value-of select="$by" />             <xsl:call-template name="string-replace-all">                 <xsl:with-param name="text"                     select="substring-after($text,$replace)" />                 <xsl:with-param name="replace" select="$replace" />                 <xsl:with-param name="by" select="$by" />             </xsl:call-template>         </xsl:when>         <xsl:otherwise>             <xsl:value-of select="$text" />         </xsl:otherwise>     </xsl:choose> </xsl:template> 

But result is not what I expect. Replacement is OK but it doesn’t keep elements/nodes – result of replacement is not an attribute. Structure and result is:

<gmd:hierarchyLevel>     <gmd:MD_ScopeCode>http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#MD_ScopeCodeserviceservice</gmd:MD_ScopeCode> </gmd:hierarchyLevel> 

How can I Made it to keep elements structure?

Thank you very much

Add Comment
1 Answer(s)

Couldn’t you do simply:

<xsl:template match="@codeList">     <xsl:attribute name="codeList">         <xsl:text>http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#</xsl:text>         <xsl:value-of select="substring-after(., '#')"/>     </xsl:attribute>     </xsl:template> 

To limit this to codelist attributes that have the specified URL, you could change:

<xsl:template match="@codeList"> 

to:

<xsl:template match="@codeList[starts-with(., 'https://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml')"> 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.