tags:

views:

52

answers:

3

Hello, I don't know anything about XSLT but I need it for a one time thing. This should be pretty simple to do. What would the XSLT need to be to take the following input and display as what is shown below.

INPUT:

<TestResult>
<set-value idref="account_lockout_duration_var">900</set-value>
<set-value idref="account_lockout_threshold_var">5</set-value>
    <group>
      <id>345</id>
      <id>265</id>
      <field>true</field>
      <message>dont do that</message>
    </group>
    <group>
      <id>333</id>
      <field>false</field>
    </group>
</TestResult>

OUTPUT

345,265,true
333,false

This is just a snippet, there can only be one field element per group but id elements are unbound.

I modified the input, using the below answers, I get extra output (everything is output, when I only want the id and field elements. thanks.

+1  A: 

It would be something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" encoding="UTF-8" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="group">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="id">
    <xsl:apply-templates/>
    <xsl:text>;</xsl:text>
  </xsl:template>
  <xsl:template match="field">
    <xsl:apply-templates/>
    <xsl:text>
</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Rob
thanks, see my edits.
+2  A: 

I would do something like this:

XML:

<TestResult>
  <set-value idref="account_lockout_duration_var">900</set-value>
  <set-value idref="account_lockout_threshold_var">5</set-value>
  <group>
    <id>345</id>
    <id>265</id>
    <field>true</field>
    <message>dont do that</message>
  </group>
  <group>
    <id>333</id>
    <field>false</field>
  </group>
</TestResult>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="/TestResult/group">
    <xsl:apply-templates/>
    <xsl:if test="following-sibling::group">
      <xsl:text>&#xA;</xsl:text>  
    </xsl:if>
  </xsl:template>

  <xsl:template match="/TestResult/group/id|field">
    <xsl:value-of select="."/>
    <xsl:if test="following-sibling::id or following-sibling::field">,</xsl:if>
  </xsl:template>

</xsl:stylesheet>

OUT:

345,265,true
333,false
DevNull
thanks, see my edits.
I updated my answer for your edits.
DevNull
@DevNull: I think your rule `xsl:template match="node()|@*"` isn't really needed. See my answer for more compact code.
Alejandro
+1  A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="text"/>
    <xsl:template match="group/*[not(self::message)]">
        <xsl:value-of select="concat(.,substring(',&#xA;',
                                                 1 + boolean(self::field),
                                                 1))"/>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

Output:

345,265,true
333,false
Alejandro
@Alejandro: Nice compact code. +1
DevNull