StellaMaris
StellaMaris

Reputation: 867

Cant select elment from xml with xslt

I have a specific Xml

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Recipient" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

and a simple xslt file. If I try to select the value of the attribute "name" from the tags which named "element" the output is nothing.

<xsl:stylesheet version = '1.0'
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
    <xsl:template match="element">
        <xsl:value-of select="@name"/>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 51

Answers (1)

har07
har07

Reputation: 89295

element in your XML is in the namespace bound to prefix xs, so match="element" didn't find a match. You need to define xs prefix (or any prefix name, as long as it is mapped to the correct namespace URI) in your XSLT and use match="xs:element" :

<xsl:stylesheet version = '1.0'
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
    <xsl:template match="xs:element">
        <xsl:value-of select="@name"/>
    </xsl:template>
</xsl:stylesheet>

xsltransform.net demo

Upvotes: 1

Related Questions