2

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.

3 Answers 3

2

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">
  <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
Sign up to request clarification or add additional context in comments.

1 Comment

I think your rule xsl:template match="node()|@*" isn't really needed. See my answer for more compact code.
1

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>

Comments

1

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.