0

I'm trying to transform the Id attribute in the root node of the XML to insert it in the Id node using an XSLT. Whenever I remove the xmlns:xsi and xmlns tags from the XML it can get the id without any issues but when it's added I'm unable to get the id.

I have tried using <xsl:value-of select="parent::node()/@Id" /> and using <xsl:value-of select="Test/@Id" /> but both don't work.

XML to transform:

<?xml version="1.0" encoding="UTF-8"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="ebee5c11-3060-475d-b3d9-e3db599da115" xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
</Test>

My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl"
                xmlns:user="urn:my-scripts"
                version="1.0">
    <msxsl:script language="C#"
                implements-prefix="user">
        <msxsl:assembly name="System" />
        <msxsl:using namespace="System" />
        <![CDATA[
     public string GuidNew()
     {
       return Guid.NewGuid().ToString("B");
     }
      ]]>
    </msxsl:script>
    <xsl:variable name="GUID" select="user:GuidNew()" />
    <xsl:template match="/">
      <Envelope>
            <Header>
                <MessageId>
            <xsl:value-of select="$GUID" />
                </MessageId>
          <Action>http://schemas.microsoft.com/dynamics/2008/01/services/TestService/create</Action>
            </Header>
            <Body>
                <MessageParts>
                    <Test xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Test">
                    <!--Test-->
                        <Test class="entity">
                            <Id>
                  <xsl:value-of select="parent::node()/@Id" />
                            </Id>
                        </Test >
                    </Test>
                </MessageParts>
            </Body>
    </Envelope>
    </xsl:template>
</xsl:stylesheet>
souser
  • 1
  • If you search for "XSLT default namespace" you will find 733 answers to this question. – Michael Kay Aug 10 '20 at 08:31
  • Though the specific error in your code is nothing to do with the namespace: `parent::node()/@Id` selects nothing because your context item is the root node, which does not have a parent. Your other attempt, `Test/@id`, fails because `Test` is in a namespace. – Michael Kay Aug 10 '20 at 08:34

0 Answers0