0

I have the following the first lines of an XML file I want to remove Id node with its children nodes inside InitgPty only.

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CstmrCdtTrfInitn>
    <GrpHdr>
      <MsgId>0001</MsgId>
      <CreDtTm>2017-10-27T07:00:53</CreDtTm>
      <NbOfTxs>1</NbOfTxs>
      <CtrlSum>84562.00</CtrlSum>
      <InitgPty>
        <Nm>ABC Co</Nm>
        <Id>
          <OrgId />
        </Id>
      </InitgPty>
    </GrpHdr>

After reading a lot I tried this XSLT but with no luck:

<xsl:stylesheet 
 version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://www.six-interbank-clearing.com/de/pain.001.001.03.ch.02.xsd" 
 xsi:schemaLocation="http://www.six-interbank-clearing.com/de/pain.001.001.03.ch.02.xsd pain.001.001.03.ch.02.xsd"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>

 <xsl:strip-space elements="*"/>


<xsl:template match="*">
     <xsl:element name="{name()}" >
       <xsl:copy-of select="@*"/>
       <xsl:apply-templates/>
     </xsl:element>
 </xsl:template>



 <xsl:template match="/*">
     <Document xsi:schemaLocation="http://www.six-interbank-clearing.com/de/pain.001.001.03.ch.02.xsd pain.001.001.03.ch.02.xsd">
       <xsl:copy-of select="@*"/>
       <xsl:apply-templates/>
     </Document >
 </xsl:template>

  <xsl:template match="InitgPty/Id"/>


</xsl:stylesheet>

I added the below line :

<xsl:template match="InitgPty/Id"/>

I need your help to achieve the result.

2 Answers 2

1

There is a simpler way to archive this, since you can match on attributes too.

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

If you want to copy comments too use:

<xsl:template match="processing-instruction()">
    <xsl:copy/>
</xsl:template>

Edit: Try this one, if the upper code is not working:

<xsl:template match="@*|node()"> 
    <xsl:choose> 
        <xsl:when test="local-name() = 'Id' and parent::node()[local-name() = 'InitgPty']"/> 
        <xsl:otherwise> 
            <xsl:copy> 
                <xsl:apply-templates/> 
            </xsl:copy> 
        </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
Sign up to request clarification or add additional context in comments.

4 Comments

The suggestion didn't work form me? I did remove my line and added yours
Weird. Are you sure, that your node has exactly that name and you havent changed the document yet? Another way to archive this would be: <xsl:template match="@*|node()"> <xsl:choose> <xsl:when test="local-name() = 'Id' and parent::node()[local-name() = 'InitgPty']"/> <xsl:otherwise> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:otherwise> </xsl:choose> </xsl:template>
This one works! Please update your answer so I can mark it as an answer. Thank you.
I guess it was because of the namespace then. Will do :)
0

The answer of @christian seems correct to me, but if it doesn't work try with an explicit prefix, for example

<xsl template match="ns:InitgPty/ns:Id" xmlns:ns='urn:iso:std:iso:20022:tech:xsd:pain.001.001.0'/>

1 Comment

Neither suggestions worked! What could be the reason?

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.