Need help outputting text nodes from XML with XSLT
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>
开发者_JAVA技巧 </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.
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>
</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
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>
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(',
',
1 + boolean(self::field),
1))"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Output:
345,265,true
333,false
精彩评论