Extracting node values using XSLT

2019-09-21 10:10发布

问题:

I have an xml like below

<Envelope>
<Body>
<response>
<timestamp>
<status>
<Objectstatus>
<class>stats</class>
<Adminstate>disabled</Adminstate>
<name>abc</name>
</objectstatus>
<objectstatus>
<class>Policy</class>
<adminstate>enabled</adminstate>
<names>xyz</name>
</Objectstatus>

The response expected is stats,disabled,abc Policy,enabled,xyz

Could you please tell me how i can do it using a stylesheet.

回答1:

First thing first, make sure your child nodes have all the same name (Adminstate or adminstate, etc...), remember that it is case sensitive.

I'm also assuming you would like a HTML output, here's what you could try:

 <xsl:for-each select="//Objectstatus">
   <p>
     <xsl:value-of select="./class"/>
     <xsl:text>,</xsl:text>
     <xsl:value-of select="./Adminstate"/>
     <xsl:text>,</xsl:text>
     <xsl:value-of select="./name"/>
   </p>
 </xsl:for-each>

Here's the output:

stats,disabled,abc Policy,enabled,xyz



标签: xslt