tags:

views:

57

answers:

5

Hi there,

I've created the following XSL stylesheet which takes an XML packet ( given to me from SVN ) and converts all the "entry" nodes, which don't include the text "archive", into a comma-separated list for my ANT script. An example XML file looks like this....

<?xml version="1.0"?>
<lists>
<list path="https://mydomain.com/branches"&gt;
<entry kind="dir">
<name>James_Work</name>
<commit revision="2209">
<author>James</author>
<date>2010-09-02T11:02:08.584250Z</date>
</commit>
</entry>
</list>
</lists>

... and my XSL is this ....

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output method="xml" indent="yes" />

<xsl:variable name="selectionPropertyList">
<xsl:for-each select="lists/list/entry[name!='archive']">svnType/<xsl:value-of select="name" />
    <xsl:if test="position()!=last()">,</xsl:if> 
</xsl:for-each>
</xsl:variable>

<xsl:template match="/">
<root>
    <svnType><xsl:copy-of select="$selectionPropertyList" /></svnType>
</root>
</xsl:template>
</xsl:stylesheet>

The problem I'm hitting is when I get an empty packet back. This might happen for example if I call the SVN branches folder which is currently empty, and end up with an empty entry in my ANT listing.

An example XML packet looks like this

<?xml version="1.0"?>
<lists>
<list path="https://mydomain.com/branches"&gt;
</list>
</lists>

Does anyone know a way I could somehow filter out these empty packets from my listings?

Thanks, James

P.S. I've tried the tips given here but it doesn't quite match what I'm trying to do here. http://www.tek-tips.com/viewthread.cfm?qid=1088712&amp;page=1

A: 

Changing your match expression to:

<xsl:template match="/lists[list/entry[name != 'archive']]">

should work.

(I'd also use apply-templates instead of the $selectionPropertyList, but that is another issue)

Nick Jones
A: 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output method="xml" indent="yes" />

<xsl:template match="list">
    <xsl:text>svnType/</xsl:text><xsl:value-of select="entry/name" />
    <xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>

<xsl:template match="/">
    <root>
        <svnType>
            <xsl:apply-templates select="//list[entry/name!='archive'][*]" />
        </svnType>
    </root>
</xsl:template>
</xsl:stylesheet>

basically [*] says that the list element should have childnodes.

Martijn Laarman
+1  A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="list/entry[name!='archive']">
        <xsl:value-of select="concat('svnType/',
                                     name,
                                     substring(',',
                                               1,
                                               position()!=last())
                                     )"/>
    </xsl:template>
    <xsl:template match="/">
        <root>
            <svnType>
                <xsl:choose>
                    <xsl:when test="lists/list/entry/name!='archive'">
                        <xsl:apply-templates/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:comment>There is no entry
                                     with "archive" name</xsl:comment>
                    </xsl:otherwise>
                </xsl:choose>
            </svnType>
        </root>
    </xsl:template>
</xsl:stylesheet>

Output:

<root>
    <svnType>svnType/James_Work</svnType>
</root>

With this input:

<lists>
    <list path="https://mydomain.com/branches"&gt;&lt;/list&gt;
</lists>

Output:

<root>
    <svnType>
        <!--There is no entry with "archive" name-->
    </svnType>
</root>
Alejandro
+1 for a good solution.
Dimitre Novatchev
This is the one I'm trying to use at the moment. Only thing is I'm getting back this kind of thing at the moment...
James Buckingham
<?xml version="1.0" encoding="UTF-8"?><root><tags>archivejcb43372010-08-31T08:56:10.756125Zmilestone 1-0-0,milestone 1-0-1,milestone 1-0-3,milestone 1-0-4,milestone 1-0-5,milestone 1-0-6,</tags></root>
James Buckingham
James Buckingham
@James Buckingham: please, post your actual input, there might be diferences.
Alejandro
A: 

Thanks everyone for the fast replies :-)

I'm not having much luck with any of these sadly, just getting strange results in the dropdowns, but I'll persist and let you know how I get on.

Might be just a case of tweaking :-).

Cheers, James

James Buckingham
A: 

Sorry for the delay in replying. Weekend got in the way :-)

@Alejandro - Here's a bit more information on the input, output I'm currently getting. Hope it helps.

Bigger example of input:

<?xml version="1.0"?>
<lists>
<list path="https://mydomain.com/website/branches"&gt;
<entry kind="dir">
   <name>ITNAM-752</name>
   <commit revision="2171">
       <author>jcb4337</author>
       <date>2010-08-30T15:13:21.006125Z</date>
   </commit>
</entry>
<entry kind="dir">
   <name>JCB4337-577</name>
   <commit revision="2171">
      <author>jcb4337</author>
      <date>2010-08-30T15:13:21.006125Z</date>
   </commit>
 </entry>
 <entry kind="dir">
     <name>JCB4337-726</name>
     <commit revision="2209">
        <author>jcb4337</author>
        <date>2010-09-02T11:02:08.584250Z</date>
      </commit>
  </entry>
  <entry kind="dir">
      <name>JCB4337-808</name>
      <commit revision="2206">
          <author>jcb4337</author>
          <date>2010-09-01T13:01:51.693625Z</date>
      </commit>
  </entry>
  <entry kind="dir">
        <name>JCB4337-847</name>
        <commit revision="2172">
            <author>jcb4337</author>
            <date>2010-08-30T15:14:12.803000Z</date>
         </commit>
  </entry>
 </list>
</lists>

The Output I'm needing to get back from the XSL is along the lines of this:

<?xml version="1.0" encoding="UTF-8"?>
     <root>
       <branches>branches/ITNAM-752,branches/JCB4337-577,branches/JCB4337-726,branches/JCB4337-808,branches/JCB4337-847</branches>
      </root>

I've used your example code above and I'm just about there I think but I'm getting some strange results along the way. Output example is:

<?xml version="1.0" encoding="UTF-8"?>
     <root>
      <branches>

          branches/ITNAM-752,
          branches/JCB4337-577,
          branches/JCB4337-726,
          branches/JCB4337-808,
          branches/JCB4337-847,

      </branches>
      </root>

So...

1) Getting a lot of white space between the list elements.

2) I've still got an extra comma at the end of the results.

3) When I try and apply this against our SVN tags folder I'm getting some odd results. Tags is slightly different because it holds this "archive" folder which I want to exclude from any results.

Currently when I output the tags folder using the same code I get this...

<?xml version="1.0" encoding="UTF-8"?>
  <root>
    <tags>


        archive

        jcb4337
        2010-08-31T08:56:51.006125Z


         tags/milestone 1-0-0,
         tags/milestone 1-0-0b,
         tags/milestone 1-0-1,
         tags/milestone 1-0-10,
         tags/milestone 1-0-11,
         tags/milestone 1-0-2,
         tags/milestone 1-0-3,
         tags/milestone 1-0-4,
         tags/milestone 1-0-5,
         tags/milestone 1-0-6,
         tags/milestone 1-0-7,
         tags/milestone 1-0-8,
         tags/milestone 1-0-9,

      </tags>
    </root>

So lots of whitespace and archive, along with all the timestamp, author information is appearing. This is using your cool solution Alejandro :-).

BTW, in case you're wondering I've got ANT doing a token replace straight after the XSL has finished. It's replacing the phrase svnType in the XML. I couldn't think of a way of applying the prefix branches / tags on each node using XSL.

Hope this all helps a bit more and thanks once again for everyone's efforts. XSL is a bit of a black art to me at the moment :-).

Cheers, James

James Buckingham