0

I have this input xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <out:ServiceResponse xmlns:out="http://test.me/datatypes">
        <out:Parties>
            <out:Party>
                <out:Field1>Value1</out:Field1>
                <out:Field2>Value2</out:Field2>
            </out:Party>
        </out:Parties>
    </out:ServiceResponse>
</soapenv:Body></soapenv:Envelope>

And this XSLT defined:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="*[local-name()='elementName']">
    <Entities>
        <xResults>
            <xsl:for-each select="ServiceResponse/Parties/Party">
                <aParty>
                    <Field1Transformed>
                        <xsl:value-of select="Field1"/>
                    </Field1Transformed>
                    <Field2Transformed>
                        <xsl:value-of select="Field2"/>
                    </Field2Transformed>
                </aParty>
            </xsl:for-each>
        </xResults>
    </Entities>
</xsl:template></xsl:stylesheet>

But when it is applied the output is a html instead of the required xml.

For my use case I need to ignore the namespaces. I can't reference "out:" or any form of placeholder over and over again.

Many thanks.

Measel
  • 41
  • 5
  • 1
    The template match in your XSLT has the strategy you would want to apply (matching on the local-name()), however it looks like this was an example/shell given as an example and you haven't replaced "elementName" with "Body". Do that, and then make similar updates to the XPaths inside of the template, matching on any element `*` and then applying the predicate filter on the `local-name()`. – Mads Hansen Jun 30 '23 at 14:25
  • Thank you @MadsHansen, this works. But I was hoping to find a way where I dont have to update all the XPaths. I actually have a large existing XSLT that I am trying to update to be more generic; I'm looking for a shortcut so I dont have to update all the existing xpaths. :( – Measel Jun 30 '23 at 14:52
  • So, what you really want is to remove the namespaces from the elements? Do you want to maintain the same `local-name()`? If so, then why post an XSLT that isn't doing that, and that doesn't even work? – Mads Hansen Jun 30 '23 at 14:54
  • 1
    In XSLT 2.0 or higher you can declare `xpath-default-namespace` once and use the local name in all your XPath expressions that refer to nodes in that namespace. In XSLT 1.0 you have no choice but to adjust XPath expressions that use the local name for nodes that are in a namespace. And I would strongly recommend you adjust them by [adding a prefix](https://stackoverflow.com/a/34762628/3016153) instead of the `*[local-name()='elementName']` hack that ignores namespaces and will bite you when two elements in different namespaces share the same local name. – michael.hor257k Jul 01 '23 at 03:40
  • P.S. *"But when it is applied the output is a html instead of the required xml."* That makes no sense and cannot be true. – michael.hor257k Jul 01 '23 at 03:54
  • 1
    Correction: Actually, you do have a choice: you could perform the transformation in two passes: first, move all nodes in a namespace to no-namespace; then transform the result using local names only. But this has the same flaw as the other method. – michael.hor257k Jul 01 '23 at 04:14
  • Thank you for all your feedback. It is all very useful. – Measel Jul 02 '23 at 18:35

0 Answers0