how to include attribute in XML tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zoom007
    New Member
    • Jun 2007
    • 6

    how to include attribute in XML tag

    I'm transforming one xml document to another using a stylesheet.
    1.) I would like to know how to include an attribute with a value in a tag, eg. how do I transform
    <tag1>hey</tag1> to
    <tag1 value1 ="1" value2="text">h ey</tag1>

    2.)the root tag of the xml document i'm transforming has an attribute.how do I instruct the XSL to retrieve data from a child of this tag using <xsl:template > element and/or <xsl:apply-templates>?

    Any ideas would be much appreciated. Thanks y'all!
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Originally posted by zoom007
    2.)the root tag of the xml document i'm transforming has an attribute.how do I instruct the XSL to retrieve data from a child of this tag using <xsl:template > element and/or <xsl:apply-templates>?

    Any ideas would be much appreciated. Thanks y'all!
    1.)
    Code:
    <xsl:template match="tag1">
    <xsl:copy>
      <xsl:attribute name="value1">1</xsl:attribute>
      <xsl:attribute name="value2">text</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>
    2. Not sure what you mean, but guessing ?
    Code:
    <xsl:template match="/*">
      <xsl:value-of select="someChild"/>
    </xsl:template>

    Comment

    • zoom007
      New Member
      • Jun 2007
      • 6

      #3
      Originally posted by jkmyoung
      1.)
      Code:
      <xsl:template match="tag1">
      <xsl:copy>
        <xsl:attribute name="value1">1</xsl:attribute>
        <xsl:attribute name="value2">text</xsl:attribute>
        <xsl:apply-templates/>
      </xsl:copy>
      </xsl:template>
      2. Not sure what you mean, but guessing ?
      Code:
      <xsl:template match="/*">
        <xsl:value-of select="someChild"/>
      </xsl:template>

      Well thanks for the reply. I tried it and it didn't work. I think I wasn't too clear on my question, so I'll explain a little more.

      I'm transforming one xml document into another, the root element in the old doc looks something like
      <root xmlns = "http://www.blah/blah">
      <element1>
      <subelem>hell o</subelem>
      <subelem2>there </subelem2>
      </element1>
      </root>

      transformation needs to look like:

      <new root xmlns:xsi="http ://www.ovaloffice" xmlns:fm="http://www.rest">
      <newElem1>
      <NewSub1>hell o</NewSub>
      <NewSub2>ther e</NewSub2>
      </newElem1>
      </new root>

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        the xmlns: attribute is actually a namespace.
        Declare a namespace in your stylesheet tag, eg add: xmlns:nms="http ://www.blah/blah"
        Everywhere you reference the node, use the namespace, eg as examples,
        <xsl:template match="nms:root ">,
        <xsl:value-of select="nms:sub elem"/>, and
        <xsl:apply-templates select="nms:ele ment1"/>

        One sample template:
        <xsl:template match"nms:eleme nt1">
        <newElem1>
        <xsl:apply-templates/>
        </newElem1>
        </xsl:template>

        Comment

        • zoom007
          New Member
          • Jun 2007
          • 6

          #5
          Originally posted by jkmyoung
          the xmlns: attribute is actually a namespace.
          Declare a namespace in your stylesheet tag, eg add: xmlns:nms="http ://www.blah/blah"
          Everywhere you reference the node, use the namespace, eg as examples,
          <xsl:template match="nms:root ">,
          <xsl:value-of select="nms:sub elem"/>, and
          <xsl:apply-templates select="nms:ele ment1"/>

          One sample template:
          <xsl:template match"nms:eleme nt1">
          <newElem1>
          <xsl:apply-templates/>
          </newElem1>
          </xsl:template>
          jkmyoung, I really appreciate your help.I tried your suggestion,but I still cant get what I want. I dont know what I'm doing wrong.here's the stylesheet and it's transformation

          <?xml version="1.0"?>
          <xsl:styleshe et version ="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
          xmlns:m="http://www.blah/blah">
          <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
          <xsl:template match ="/">
          <new_root xmlns:lm="http://iou.cm">
          <xsl:apply-templates/></new_root>
          </xsl:template>
          <xsl:template match ="m:element1 ">
          <newElem1><xsl: value-of select="m:NewSu b1"/></newElem1>
          </xsl:template>
          </xsl:stylesheet>

          here's the output:

          <?xml version="1.0" encoding="UTF-16" ?>
          <new_root xmlns:m="http://www.blah/blah xmlns:lm="http://iou.cm">
          <newElem1>hello </newElem1>
          </new_root>


          How can I remove xmlns:m ="http://www.blah/blah namespace ?
          I dont want it to appear in there

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            In the stylesheet element use attribute
            exclude-result-prefixes="m"

            Comment

            Working...