Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • SiKo279 82 posts 238 karma points
    Nov 04, 2009 @ 15:43
    SiKo279
    0

    Generating xml with xslt, output manipulation

    Hi,

    I want to ouput some xml code that should look like this:

    [code]
    <menu> 
     <entry title='Local Service' link='/local.aspx'></entry>
     <entry title='Estimates' link='/locksmith-costs-estimates.aspx'></entry>
     <entry title='Security Tips' link='/security-tips.aspx'></entry>
     <entry title='FAQ' link='/faq.aspx'></entry>
     <entry title='Contact' link='/contact-locksmith-service.aspx'></entry>
     <entry title='About Us' link='/about-us.aspx'></entry>
    </menu>
    [/code]

    But instead I get this:

    [code]
    <menu><entry title="Local Service" link="/local.aspx" /><entry title="Estimates" link="/locksmith-costs-estimates.aspx" /><entry title="Security Tips" link="/security-tips.aspx" /><entry title="FAQ" link="/faq.aspx" /><entry title="Contact" link="/contact-locksmith-service.aspx" /><entry title="About Us" link="/about-us.aspx" /></menu>
    [/code]

    The xslt I use is this:

    [code]

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxml="urn:schemas-microsoft-com:xslt"
     xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
     exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">


    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>

    <!-- Input the documenttype you want here -->
    <!-- Typically '1' for topnavigtaion and '2' for 2nd level -->
    <!-- Use div elements around this macro combined with css -->
    <!-- for styling the navigation -->
    <xsl:variable name="level" select="1"/>

    <xsl:template match="/">

    <!-- The fun starts here -->
    <menu>
    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
     <entry title='{@nodeName}' link='{umbraco.library:NiceUrl(@id)}'></entry>
    </xsl:for-each>
    </menu>

    </xsl:template>

    </xsl:stylesheet>
    [/code]

     

    Any ideas on how I can change the code above to get the desired output?

    Thanks

  • SiKo279 82 posts 238 karma points
    Nov 04, 2009 @ 15:47
    SiKo279
    0

    Basically what I want is

    <menu><entry title='singlequotedtitle' link='singlequotedlink'></entry><entry title='somemoresinglequotedtitle' link='/etc/page.aspx'></entry></menu>

    And I get:

    <menu><entry title="doublequotedtitle" link="doublequotedlink: /><entry title="moretitle" link="morelink" /></menu>

    which does not work for me.

    any ideas

  • Yannick Smits 321 posts 718 karma points
    Nov 04, 2009 @ 22:22
    Yannick Smits
    0

    Other than treating the output as text and manually processing it I don't think XSLT nor .NET framework transformation classes have an option for that.

  • Chris Koiak 700 posts 2626 karma points
    Nov 04, 2009 @ 22:31
    Chris Koiak
    0

    I've not tried this, but you could play around with the 'output' method

    <

     

    xsl:output method="html"

    />

    You can also set it to 'text'.

  • SiKo279 82 posts 238 karma points
    Nov 05, 2009 @ 10:29
    SiKo279
    0

    Thanks for the replies.

    However, setting the output method to html, helps me getting the end tags back, but the main thing: I still have " in my string.

    Can I use the replace function? How would I use it?

  • James Telfer 65 posts 165 karma points
    Nov 05, 2009 @ 14:02
    James Telfer
    0

    Hi SiKo279,

    Do you have control of the parsing on the other end? XML parsers should handle single/double quotes and self-closing tags without any issues - it's part of XML itself.

    The reason I ask this is that you seem to be buying yourself a world of pain by trying to force different output.

    That said, it is possible to achieve something along these lines by using xsl:text; e.g.

        <menu>
          <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
            <xsl:text disable-output-escaping='yes'>&lt;entry title='</xsl:text>
            <xsl:value-of select='@nodeName'/>
            <xsl:text>' link='</xsl:text>
            <xsl:value-of select='umbraco.library:NiceUrl(@id)'/>
            <xsl:text disable-output-escaping='yes'>'&gt;&lt;/entry&gt;</xsl:text>
          </xsl:for-each>
        </menu>
    

    You may need to change your output from xml to html or text for this to work. I've used this technique to output Adobe Tagged Text format from an XML input document, but I didn't find it fun.

    Best to use an XML parser on the other end  - if you can.

    Cheers,
    JT

  • SiKo279 82 posts 238 karma points
    Nov 23, 2009 @ 08:55
    SiKo279
    0

    Thank you for the reply.

    I navigated away from this approach and ended up passing the (menu/navigation) data from xml to some flat delimited text format.

    If in the future, data structures should become more complex, I will use the code you posted a try.

    The problem was that the parameter in html is specified with "<content>". If the content contains ", the string gets terminated prematurely. Hence I wanted to replace them with ' ... Maybe I'm still missing something?

    I was specifying the 'InitParams" property of a silverlight object. Below is the code and format of the quick and dirty route I took:

        <object id= ...

    <param name="InitParams" value=
    "
    menuEntries=Local Service|/local.aspx*
    Estimates|/locksmith-costs-estimates.aspx*
    Security Tips|/security-tips.aspx*
    FAQ|/faq.aspx*
    Contact|/contact-locksmith-service.aspx*
    About Us|/about-us.aspx*
    "
    />

    I wanted to to pass a string containing xml (see first posts) and use linq to access the data.

    All fine now though, but still a bit old fashioned :)

    Thanks for your thoughts!

Please Sign in or register to post replies

Write your reply to:

Draft