XSLT: Confusion over adding attributes with xml:element and xml:attribute

Discussion in 'XML' started by Jordan Willms, Nov 8, 2004.

  1. Hi there.

    I am working with lom metadata and I am a little confused with how to
    form the following xml element:

    <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_v1p2
    imsmd_v1p2p2.xsd"/>

    I have the following so far:

    <xsl:element name="lom">
    <xsl:attribute namespace="xmlns"
    name="xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>

    <xsl:apply-templates/>
    </xsl:element>

    but it creates some very strange results. Does anyone know how I could go
    about forming this tag? I know I could do it withou xsl:element, but I'd
    like to do everything by the books.

    Any help would be greatly appretiated.

    Cheers!

    ...jordan willms
     
    Jordan Willms, Nov 8, 2004
    #1
    1. Advertising

  2. (Jordan Willms) writes:

    > Hi there.
    >
    > I am working with lom metadata and I am a little confused with how to
    > form the following xml element:
    >
    > <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_v1p2
    > imsmd_v1p2p2.xsd"/>
    >
    > I have the following so far:
    >
    > <xsl:element name="lom">
    > <xsl:attribute namespace="xmlns"
    > name="xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>
    >
    > <xsl:apply-templates/>
    > </xsl:element>
    >
    > but it creates some very strange results.


    actually you shouldn't get a result at all, it is a syntax error to try
    to generate an attribute with name xmlns. XML namespace declarations
    are not attributes as far as Xpath is concerned, they can't be generated
    with xsl:attribute, they don't show up if you select @*.

    Just generate the attribute that you need (xsi:schemaLocation) any
    needed xml namespace declarations will be generated automatically.

    > Does anyone know how I could go
    > about forming this tag? I know I could do it withou xsl:element, but I'd
    > like to do everything by the books.


    Not sure what you mean by "by the books" I'd expect most xslt books to
    advise thatyou don't need to use xsl:attribute or xsl:element unless you
    need to generate the element or attribute names dynamically.
    The main idea of xslt is that the templates should look like a
    "template" for part of your result document, so it would b emore natural
    to just use the form you give at the top rather than using xsl:element.

    >
    > Any help would be greatly appretiated.
    >
    > Cheers!
    >
    > ..jordan willms


    David
     
    David Carlisle, Nov 8, 2004
    #2
    1. Advertising

  3. In article <>,
    Jordan Willms <> wrote:

    >I know I could do it withou xsl:element, but I'd
    >like to do everything by the books.


    By what book? There is no good reason to use xsl:element when you
    could use a literal result element. You're just making life
    unnecessarily complicated.

    -- Richard
     
    Richard Tobin, Nov 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page