0

I am having task to extract a part of XML file from my source XML file. I am new to XSLT part but i tried to write XSLT but not getting any luck in fetching right data.

For example I am having XML like

> <?xml version="1.0" encoding="utf-8"?>
<Response xmlns="https://insuredapp.com/rating/" id="24d846677aefd1bd7fdf52286e692a22">
    <Result type="request">
        <abc>
            <bcd>

            </bcd>
        </abc>
    </Result>
    <Result type="response">
        <InsuranceSvcRs>
            <PersPkgPolicyQuoteInqRs>
                <PersPolicy>
                    <Coverage>
                        <!--- Some MOre Elemetns -->
                    </Coverage>
                    <Coverage>
                        <!--- Some MOre Elemetns -->
                    </Coverage>
                    <Coverage>
                        <!--- Some MOre Elemetns -->
                    </Coverage>
                </PersPolicy>
            </PersPkgPolicyQuoteInqRs>
        </InsuranceSvcRs>
    </Result>
</Response>

After applying XSLT transform I want output to be

> <?xml version="1.0" encoding="utf-8"?>
<Coverages>
    <Coverage>
        <!--- Some More Elements -->
    </Coverage>
    <Coverage>
        <!--- Some More Elements -->
    </Coverage>
    <Coverage>
        <!--- Some More Elements -->
    </Coverage>
</Coverages>

To extract above coverages using XSLT I had written following XSLT Code

> <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/Response/Result[@type='response']/InsuranceSvcRs/PersPkgPolicyQuoteInqRs/PersPolicy">
        <!--<xsl:for-each select="/Response/Result[@type='response']/InsuranceSvcRs/PersPkgPolicyQuoteInqRs/PersPolicy/Coverage">
            <xsl:value-of select="CoverageCd"/>
        </xsl:for-each>-->

        <xsl:copy>
            <xsl:apply-templates select="/Response/Result[@type='response']/InsuranceSvcRs/PersPkgPolicyQuoteInqRs/PersPolicy"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

But I am not getting desired output kindly look into it.

0

1 Answer 1

1

First of all, there is a default namespace in your input document:

<Response xmlns="https://insuredapp.com/rating/">

If you'd like to select individual elements from this input, you have to redeclare this namespace in the XSLT stylesheet:

<xsl:stylesheet xmlns:resp="https://insuredapp.com/rating/">

Start with a template that matches / (the document node), output Coverages and use the xsl:copy-of instruction.

XSLT Stylesheet

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:resp="https://insuredapp.com/rating/"
    exclude-result-prefixes="resp">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
       <Coverages xmlns="https://insuredapp.com/rating/">
          <xsl:copy-of select="//resp:Coverage"/>
       </Coverages>
    </xsl:template>

</xsl:stylesheet>

XML Output

<?xml version="1.0" encoding="UTF-8"?>
<Coverages xmlns="https://insuredapp.com/rating/">
   <Coverage>
      <!--- Some MOre Elemetns -->
   </Coverage>
   <Coverage>
      <!--- Some MOre Elemetns -->
   </Coverage>
   <Coverage>
      <!--- Some MOre Elemetns -->
   </Coverage>
</Coverages>

Note that the elements in the output are still in a namespace. It is not clear whether you'd like to get rid of that namespace.

Sign up to request clarification or add additional context in comments.

1 Comment

@Faiz If this answer was helpful to you, then please accept it. Thanks! Here is how accepting an answer works: meta.stackexchange.com/questions/5234/….

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.