Extract a subset of XML file using XSL
Asked Answered
C

4

11

I have this XML file:

<Response>
    <errorCode>error Code</errorCode>
    <errorMessage>msg</errorMessage>
    <ResponseParameters>
        <className>
            <attribute1>a</attribute1>
            <attribute2>b</attribute2>
        </className>
    </ResponseParameters>
</Response>

And I want the output to be:

<className>
    <attribute1>a</attribute1>
    <attribute2>b</attribute2>
</className>

My current XSL file is including also "ResponseParameters" tag, which I do not want.

EDIT: note that the node className is dynamic. I do not know what will be this name at runtime.

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output indent="yes" />

    <xsl:template match="/">
        <xsl:copy-of select="//ResponseParameters">
        </xsl:copy-of>
    </xsl:template>
</xsl:stylesheet>
Clari answered 25/2, 2009 at 16:1 Comment(0)
A
16

Use :

<xsl:copy-of select="/Response/ResponseParameters/node()"/>

The "//" abbreviation is very expensive (causes the complete XML document to be scanned), and should be avoided.

Alix answered 27/2, 2009 at 15:33 Comment(0)
N
2
<xsl:copy-of select="Response/ResponseParameters//*"/>
Nonstriated answered 25/2, 2009 at 16:13 Comment(0)
B
1

One way is to pass a parameter containing the node name into the XSLT and use the parameter passed in with the name() function to match the dynamic node.

Edit:

But in this simple case either of the other answers suggesting ResponseParameters//* or ResponseParameters/* are a far simpler solution.

Breakwater answered 25/2, 2009 at 16:13 Comment(0)
G
-1

May be I did something wrong, but I had to use two templates for XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="no" />

<xsl:template match="/Response/ResponseParameters">
    <xsl:copy-of select="node()"/>
</xsl:template>

<xsl:template match="/">
 <xsl:apply-templates select="/Response/ResponseParameters"/>
</xsl:template>
</xsl:stylesheet> 

If I skipped the "template match="/"" suddenly there appeared a strange text:

error Code
msg

I use this XSL processor:

xsltproc $xsl_file $xml_file > $htm_file
if [ $? -eq 0  ]; then # This just activates web browser with the html file
    xdotool windowactivate $(xdotool search "epiphany" | tail -n 1)
fi
Gothart answered 20/5 at 4:7 Comment(5)
You are making this unnecessarily complicated by not following the accepted answer. (And the text is not "strange": it is the text from the input XML copied to the output by the built-in template rules.)Shrug
Dear Michael, I tried to repeat the problem according the text. It was not working, I faced the problem. So I posted solution. XSL is very complicated, so a bit clarification is hopefully helpful.Gothart
All you need is a single template that does: <xsl:template match="/"><xsl:copy-of select="Response/ResponseParameters/*"/></xsl:template>.Shrug
Yes, you are right. My version is really unnecessarily complicated. Your version is simpler (if it is inside other folklore of XSL template of course.) I was puzzled by the strange "error Code, msg" which appeared I did not know where from. Thanks. :-)Gothart
You may want to read (at least) this: lenzconsulting.com/how-xslt-worksShrug

© 2022 - 2024 — McMap. All rights reserved.