Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting XML to Text File 1

Status
Not open for further replies.

TomKane

Programmer
Jul 24, 2001
1,018
AU
Hi,

I'm attempting to convert an XML document to text and I would be grateful for any advice that you may have.

I have an XML document that uses node attributes quite a lot - the attribute name is always "value" even though the tag name can differ - below is an example:

<Facility>
<Header>
<Action value="Create"/>
</Header>
<Key>
<BAAPID value="TX34"/>
<PRCD value="920431"/>
</Key>
<Data>
<ALTYNA value="TX PDT"/>
<ISTYALID value="TX34-USD-LP-F"/>
<COFL value="N"/>
</Data>
<Event>
<Header>
<Action value="Create"/>
</Header>
<Key>
<EVURRE value="limit amount"/>
</Key>
<Data>
<EVSETYNA value="LINE-EVENTS"/>
<EVTYNA value="START"/>
<EVAM value="-7000000"/>
<EVDA value="2006-05-31"/>
<EVCU value="USD"/>
</Data>
</Event>
<Event>
<Header>
<Action value="Create"/>
</Header>
<Key>
<EVURRE value="limit expiry"/>
</Key>
<Data>
<EVSETYNA value="LINE-EVENTS"/>
<EVTYNA value="END"/>
<EVAM value="0"/>
<EVDA value="2006-06-30"/>
<EVCU value="USD"/>
</Data>
</Event>
<Role>
<Header>
<Action value="Create"/>
</Header>
<Key>
<QROCD value="BOR"/>
<RIENALTYNA value="TX34 CST NR"/>
<RIENALID value="501882"/>
</Key>
<Data>
<ISROPTPC value="100"/>
</Data>
</Role>
<Role>
<Header>
<Action value="Create"/>
</Header>
<Key>
<QROCD value="TAKER"/>
<RIENALTYNA value="TX34 RT NR"/>
<RIENALID value="501882"/>
</Key>
<Data>
<ISROPTPC value="100"/>
</Data>
</Role>
<Udf>
<Header>
<Action value="Create"/>
</Header>
<Key>
<UDTYNA value="LEGAL ENTITY"/>
</Key>
<Data>
<UDDA value="LEGAL ENTITY DESCRIPTION"/>
</Data>
</Udf>
</Facility>

What I'm trying to do is create a stylesheet that can extract the value attribute and but it into a text file. The Style sheet that I've created is as below:

<?xml version="1.0"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "<xsl:eek:utput method="text"/>
<xsl:template match="Facility">
<xsl:value-of select="/Facility/Key/BAAPID/@value"/>
<xsl:text>@value</xsl:text>
</xsl:template>
</xsl:stylesheet>

I'm trying to start small by just being able to extract one attribute - if I could even do that at least I'd be on my way to getting through this.

Any advice would be greatly appreciated. Apologies if my question appears obvious.

Thanks and regards,
Tom
 
But the stylesheet is working, is it not?
 
This is a quick minimal xslt to get all the "value" attributes exported to text file, if that's the meaning of the question.
[tt]
<?xml version="1.0"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "<xsl:eek:utput method="text" encoding="utf-8"/>
<xsl:template match="node()">
<xsl:for-each select="@*[name()='value']">
<xsl:value-of select="."/><xsl:text>&#x0d;&#x0a;</xsl:text>
</xsl:for-each>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
[/tt]
 
Hi tsuji,

Unfortunately it is not working what I get is something like the following:

@value@value@value@value...... etc etc

It's giving me the literal @value rather than the content of the value attribute.

Thanks for posting,
Tom

 
Hi tsuji,

Please ignore the previous post - that was for your first post. The stylesheet you gave me extracted the value attributes brilliantly - thanks a million for that - it's much appreciated!

Thanks again,
Tom
 
Thanks, Tom. But there is still some interest in looking into your first post.

[1] I said it is working because you should see output like this.
[tt]TX34@value[/tt]
At least, the first part should get the value all right. It should not be empty---unless your Facility is not really the root. You use absolute path
<xsl:value-of select="/Facility/Key/BAAPID/@value"/>
It should be met. However, it is better be.
[tt]<xsl:value-of select="Key/BAAPID/@value"/>[/tt]
But the main thing is it should have matched and displayed TX34.

[2] The literal @value should be expected. Not only because the path is no good. The main reason is also that xsl:text element is _not_ capable of being a container. It can only be a leaf. Hence, no matter @value or {@value} or variants would only be treated as literal. Furthermore, how about this, one may say?
[tt]<xsl:text><xsl:value-of select="etc etc" /></xsl:text> <!-- wrong -->[/tt]
But this would be an error for reason as said.

 
Hi tsuji,

You are correct - I changed the line as below:

Was:
<xsl:value-of select="/Facility/Key/BAAPID/@value"/>

Now:
<xsl:value-of select="Key/BAAPID/@value"/>

and that gave me the value of TX34@value

The reason I put @value between the <xsl:text></xsl:text> tags was because I mistakenly thought that was how I would output the attribute - I hadn't realised the problem with the path.

Again, thanks for your help.... I do have another question and you've helped so much that I understand if your response is BTW or RTM...

There are several <Facility> tags in a real document and I was wondering if it is possible to only apply the <xsl:text>&#x0d;&#x0a;</xsl:text> after </Facility> rather than after every attribute.

Best regards,
Tom

 
>if it is possible to only apply the <xsl:text>&#x0d;&#x0a;</xsl:text> after </Facility> rather than after every attribute.
Like this.
[tt]
<xsl:template match="node()">
<xsl:for-each select="@*[name()='value']">
<xsl:value-of select="."/>
[green]<xsl:if test="name(..)='Facility'">
<xsl:text>&#x0d;&#x0a;</xsl:text>
</xsl:if>
<xsl:if test="name(..)!='Facility'">
<xsl:text>&#x09;</xsl:text>
</xsl:if>[/green]
</xsl:for-each>
<xsl:apply-templates />
</xsl:template>
[/tt]
Tab (&#x09;) is taken as otherwise separator; you're free to choose any.
 
Correction

The above only work, somehow, if Facility element has value attribute! This is the correct version for general case.
[tt]
<xsl:template match="node()">
[green]<xsl:if test="name()='Facility'">
<xsl:text>&#x0d;&#x0a;</xsl:text>
</xsl:if>[/green]
<xsl:for-each select="@*[name()='value']">
<xsl:value-of select="."/>
[green]<xsl:text>&#x09;</xsl:text>[/green]
</xsl:for-each>
<xsl:apply-templates />
</xsl:template>
[/tt]
 
Hi tsuji,

That is really top drawer stuff you've done for me today. Many thanks. I certainly have a lot to learn with this stuff!!

Best regards,
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top