Need advice: What is the best way; CDATA or normal xml tag

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SM

    Need advice: What is the best way; CDATA or normal xml tag



    Hello,
    I have an XML file that looks like this

    <?xml version="1.0" encoding="UTF-8"?>
    <discography>
    <CD>
    <title>Moonligh t</title>
    <year>1974</year>

    <description>
    <p>Descriptio n <span>of</spanthe CD</p>
    <p>More description of the CD</p>
    <p>Another <b>paragraph</bof the CD</p>
    </description>

    <item>
    <track>Foreve r</track>
    <track>Again</track>
    <track>Alone</track>
    </item>
    </CD>
    </discography>

    The HTML output should look like this:

    <h1>Moonlight </h1>
    <h3>1974</h3>

    <p>Descriptio n <span>of</spanthe CD</p>
    <p>More description of the CD</p>
    <p>Another <b>paragraph</bof the CD</p>

    <ul>
    <li>Forever</li>
    <li>Again</li>
    <li>Alone</li>
    </ul>

    I've manage to create the HTML but i'm stock with the xml
    <description> .
    I found a solution using CDATA, but i'm not sure if it's an elegant
    solution. I've read that the CDATA was not ment to be used for that
    purpose.

    As a programmer, i always try to create code that is ok and elegant.

    But in this case, the CDATA works perfectly and it's so simple. I feel
    like if don't use the CDATA solution i would need to create a
    complicated function to extract all those HTML tags inside the xml
    <descriptiontaq .

    What should i do? Any suggestions?


    Solution:
    <description>
    <![CDATA[
    <p>Descriptio n <span>of</spanthe CD</p>
    <p>More description of the CD</p>
    <p>Another <b>paragraph</bof the CD</p>
    ]]>
    </description>

    Thanks in advance for all your advice
    Marco
  • Joseph J. Kesselman

    #2
    Re: Need advice: What is the best way; CDATA or normal xml tag

    <![CDATA[]]is just text content, as far as any properly written XML
    application is concerned. It's an alternative to individually escaping
    characters, <![CDATA[<&>]]is precisely equivalent to the &lt;&amp;&gt ;
    sequence. That's all it is.

    If you think you need to put XML/HTML content into a CDATA section, you
    are almost undoubtedly wrong. You should be able to just produce the
    proper document structure directly.

    You didn't say what tools you're using, so it's hard to give you
    specific advice... but XSLT would have no trouble generating your
    desired output, nor should any other halfway reasonable XML tooling.

    Comment

    • SM

      #3
      Re: Need advice: What is the best way; CDATA or normal xml tag

      On May 9, 11:35 am, "Joseph J. Kesselman" <keshlam-nos...@comcast. net>
      wrote:
      <![CDATA[]]is just text content, as far as any properly written XML
      application is concerned. It's an alternative to individually escaping
      characters, <![CDATA[<&>]]is precisely equivalent to the &lt;&amp;&gt ;
      sequence. That's all it is.
      >
      If you think you need to put XML/HTML content into a CDATA section, you
      are almost undoubtedly wrong. You should be able to just produce the
      proper document structure directly.
      >
      You didn't say what tools you're using, so it's hard to give you
      specific advice... but XSLT would have no trouble generating your
      desired output, nor should any other halfway reasonable XML tooling.
      Thanks for the explanation. Im using SimpleXML and PHP.

      Marco

      Comment

      • SM

        #4
        Re: Need advice: What is the best way; CDATA or normal xml tag

        On May 9, 11:35 am, "Joseph J. Kesselman" <keshlam-nos...@comcast. net>
        wrote:
        <![CDATA[]]is just text content, as far as any properly written XML
        application is concerned. It's an alternative to individually escaping
        characters, <![CDATA[<&>]]is precisely equivalent to the &lt;&amp;&gt ;
        sequence. That's all it is.
        >
        If you think you need to put XML/HTML content into a CDATA section, you
        are almost undoubtedly wrong. You should be able to just produce the
        proper document structure directly.
        >
        You didn't say what tools you're using, so it's hard to give you
        specific advice... but XSLT would have no trouble generating your
        desired output, nor should any other halfway reasonable XML tooling.
        Thanks for the explanation. I'm using SimpleXML and PHP to read the
        xml file. It's all good except the <descriptiont ag with all those
        embedded HTML tags that i'm stock with.
        I've search and search and found CDATA as a probable solution but was
        never sure of it. Now that i'm reading you, it's obvious that it's not
        the path to take. Maybe you could guide me on how to generate proper
        HTML using simpleXML in PHP.

        Thanks
        Marco

        Comment

        • Joseph J. Kesselman

          #5
          Re: Need advice: What is the best way; CDATA or normal xml tag

          SM wrote:
          Maybe you could guide me on how to generate proper
          HTML using simpleXML in PHP.
          I haven't used either simpleXML or PHP, so I'll have to refer this to
          someone else.

          Comment

          • SM

            #6
            Re: Need advice: What is the best way; CDATA or normal xml tag

            On May 9, 12:59 pm, "Joseph J. Kesselman" <keshlam-nos...@comcast. net>
            wrote:
            SM wrote:
            Maybe you could guide me on how to generate proper
            HTML using simpleXML in PHP.
            >
            I haven't used either simpleXML or PHP, so I'll have to refer this to
            someone else.
            ok. Thanks

            Comment

            • SM

              #7
              Re: Need advice: What is the best way; CDATA or normal xml tag

              On May 9, 1:46 pm, SM <servandomont.. .@gmail.comwrot e:
              On May 9, 12:59 pm, "Joseph J. Kesselman" <keshlam-nos...@comcast. net>
              wrote:
              >
              SM wrote:
              Maybe you could guide me on how to generate proper
              HTML using simpleXML in PHP.
              >
              I haven't used either simpleXML or PHP, so I'll have to refer this to
              someone else.
              >
              ok. Thanks
              Got it! After a couple of hours researching i finally found a
              solution. And elegant solution and one that, as a programmer, i feel
              confortable using.

              The trick is to use the funtion in PHP asXML(); This function returns
              a well-formed XML string based on SimpleXML element

              Here's a good article on how it works (search for the paragraph 'Edge
              Conditions')

              I've tried it and it works!

              Marco

              Comment

              • SM

                #8
                Re: Need advice: What is the best way; CDATA or normal xml tag

                On May 9, 6:05 pm, SM <servandomont.. .@gmail.comwrot e:
                On May 9, 1:46 pm, SM <servandomont.. .@gmail.comwrot e:
                >
                On May 9, 12:59 pm, "Joseph J. Kesselman" <keshlam-nos...@comcast. net>
                wrote:
                >
                SM wrote:
                Maybe you could guide me on how to generate proper
                HTML using simpleXML in PHP.
                >
                I haven't used either simpleXML or PHP, so I'll have to refer this to
                someone else.
                >
                ok. Thanks
                >
                Got it! After a couple of hours researching i finally found a
                solution. And elegant solution and one that, as a programmer, i feel
                confortable using.
                >
                The trick is to use the funtion in PHP asXML(); This function returns
                a well-formed XML string based on SimpleXML element
                >
                Here's a good article on how it works (search for the paragraph 'Edge
                Conditions')
                >
                I've tried it and it works!
                >
                Marco
                Opppsss ! to excited, i forgot the article link:

                Comment

                Working...