xmlxsltxslt-1.0xsl-stylesheet

select value of an element which has a namespace assigned


i'm trying to do a simple xsl transformation on an edifabric x12 xml file. How can i select the <D_744_1> Element?

Sample XML:

<INTERCHANGE xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="www.edifabric.com/x12">
    <S_ISA>
        <D_744_1>00</D_744_1>
    </S_ISA> 
</INTERCHANGE>

Sample XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <testfield><xsl:value-of select="INTERCHANGE/S_ISA/D_744_1" /></testfield>
    </xsl:template>
</xsl:stylesheet>

Result:

<?xml version="1.0" encoding="utf-8"?>
<testfield/>

Desired Result:

 <?xml version="1.0" encoding="utf-8"?>
    <testfield>00</testfield>

updated answer thanks @ChriPf:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:edi="www.edifabric.com/x12" exclude-result-prefixes="edi">

    <xsl:template match="edi:INTERCHANGE">
        <testfield><xsl:value-of select="edi:S_ISA/edi:D_744_1" /></testfield>
    </xsl:template>

</xsl:stylesheet>

Solution

  • Your solution could look like this:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:edi="www.edifabric.com/x12">
      <xsl:template match="edi:D_744_1">
        <xsl:element name="testfield">
            <xsl:copy-of select="." />
        </xsl:element>
    </xsl:template>
    </xsl:stylesheet>
    

    If you have a default namespace in your xml, you have to define it in the xsl as well. Find some more information e.g. here.