Nikita
Nikita

Reputation: 3

How to read the specific fields from the XML payload, where this payload is coming again in the field of the main XML payload

Need help to read the XML payload which is coming inside one of the field.

Request Payload:

<?xml version="1.0" encoding="UTF-8"?>
<Code xmlns:ns0="http://example.com">
   <Header>
      <Field1>111</Field1>
      <Field2>text</Field2>
   </Header>
   <Trailer>
     &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
        <Field4>
            <Field5>
               <Field6>Age</Field6>
                 <Field7>Location</Field7>
               <Field6>Age</Field6>
                 <Field7>Location</Field7>
            </Field5>
        </Field4>
   </Trailer>
</Code>

Above is the Request XML payload in which we have field called <Trailer> where we are again getting an XML payload. We need to fetch only the selected fields from the XML payload which is in the <Trailer> field and generate below Output.

Expected Output Payload:

<?xml version="1.0" encoding="UTF-8"?>
<Code xmlns:ns0=http://example.com>
   <Header>
      <Field1>111</Field1>
      <Field2>text</Field2>
   </Header>
   <Field5>
        <Field6>Age</Field6>
            <Field7>Location</Field7>
         <Field6>Age</Field6>
             <Field7>Location</Field7>
    </Field5>
</Code>

I tried with below XSLT but I am unlucky to fetch the required fields from the <Trailer> field

XSLT Code:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Code>
 <xsl:copy-of select="//Header" />
 <xsl:value-of select="//Trailer" disable-output-escaping="yes"/>

</Code>
</xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 163

Answers (2)

John Ernst
John Ernst

Reputation: 1235

Try this.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

  <xsl:template match="Trailer">
    <xsl:apply-templates select="Field4/Field5"/>
  </xsl:template>

</xsl:stylesheet>

Upvotes: 1

y.arazim
y.arazim

Reputation: 3250

The question is not entirely clear. The expected result can be produced using:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Trailer">
    <xsl:apply-templates select="Field4/Field5"/>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions