Creating an Element with Text and Attributes
Now we’ll create an element that has both text and
attributes. Remember, in order to create attributes, we have to create
an <xs:complexType>
, so we
have to create a new type based on xs:string
. Here’s how we do that:
<?xml version="1.0" encoding="UTF-8"?> <!-- content2.xsd --> <xs:schema xmlns="http://www.oreilly.com/xslt" targetNamespace="http://www.oreilly.com/xslt" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="content2"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="color” type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:schema>
Our new element is a complex type, but it uses what XML Schema
refers to as simple content. In other words, this
element is an extension of a simple type, xs:string
. The extension to the simple type
is that we’re adding an attribute. Here’s how our document looks
now:
<?xml version="1.0" encoding="utf-8"?> <!-- content2.xml --> <content2 xmlns="http://www.oreilly.com/xslt" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oreilly.com/xslt content2.xsd" color="blue"> Our element now contains some text! It's getting more useful all the time. </content2>
Get XSLT, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.