0

I have the following xml

<?xml version="1.0" encoding="UTF-8"?>
<metadata xml:lang="en">
    
   <gmd:GEMINI_Metadata xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:srv="http://www.isotc211.org/2005/srv" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <itemName Sync="TRUE">Wrong Title</itemName>
     <gmd:identificationInfo>
        <gmd:MD_DataIdentification>
            <gmd:abstract>
              <gco:CharacterString>AbstractText.</gco:CharacterString>
            </gmd:abstract>
            <gmd:citation>
              <gmd:CI_Citation>
                <gmd:title>
                  <gco:CharacterString>Correct Title</gco:CharacterString>
                </gmd:title>
              </gmd:CI_Citation>
            </gmd:citation>
        </gmd:MD_DataIdentification>
    </gmd:identificationInfo>
   </gmd:GEMINI_Metadata>
</metadata>

I am trying to replace the text "Wrong Title" from the itemName tag with the text "Correct Title" from the gmd:title/gco:CharacterString tag.

I've tried to use the following xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!--IDENTITY TRANSFORMATION TEMPLATE NEEDED-->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="itemName">
        <itemName Sync="TRUE">
            <xsl:copy-of select="gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString"/>
        </itemName>      
    </xsl:template>
</xsl:stylesheet>

So the resulting xml should look like this

<?xml version="1.0" encoding="UTF-8"?>
<metadata xml:lang="en">
    <itemName Sync="TRUE">Correct Title</itemName>
   <gmd:GEMINI_Metadata xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:srv="http://www.isotc211.org/2005/srv" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <gmd:identificationInfo>
        <gmd:MD_DataIdentification>
            <gmd:abstract>
              <gco:CharacterString>AbstractText.</gco:CharacterString>
            </gmd:abstract>
            <gmd:citation>
              <gmd:CI_Citation>
                <gmd:title>
                  <gco:CharacterString>Correct Title</gco:CharacterString>
                </gmd:title>
        <gmd:MD_DataIdentification>
     <gmd:identificationInfo>
   </gmd:GEMINI_Metadata>
</metadata>

but it does not change. What is the best way to achieve this.

3
  • 2
    Please show a minimal reproducible example, not code snippets taken out of context. Commented Jul 15, 2020 at 16:11
  • more code added. Commented Jul 15, 2020 at 19:33
  • It's still not usable code: xsltfiddle.liberty-development.net/ehW12fK. What you describe is not possible. Your stylesheet has a single template matching itemName. There is no way it can pass the entire output unchanged. Commented Jul 15, 2020 at 19:39

1 Answer 1

1

You need to match against every element and attribute, and copy them. When you match the itemName, you substitute the value you need. This xsl produces your output.

-Also, don't get tripped up on the namespaces. They need to be included in the <xsl:styleheet />

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd"  xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
<xsl:output method="xml" encoding="ISO-8859-1"/>

<xsl:template match="/">
    <xsl:apply-templates select="*" />
</xsl:template>

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

<xsl:template match="itemName">
    <xsl:copy>
        <xsl:apply-templates select="@*|*"/>
        <xsl:value-of select="../gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.