PDA

View Full Version : Stripping text from an attribute with XSL


legend166
10-22-2009, 02:41 AM
Hi, can anyone help me with this?

I've got an XML document with a bunch of elements that look like this:

<recipe href="Store_Recipe__Polenta.xml">

Is it possible to use XSL so that it only returns the "Store_Recipe__Polenta" part, and takes away the .xml part?


Thanks.

oesxyl
10-22-2009, 02:59 PM
Hi, can anyone help me with this?

I've got an XML document with a bunch of elements that look like this:

<recipe href="Store_Recipe__Polenta.xml">

Is it possible to use XSL so that it only returns the "Store_Recipe__Polenta" part, and takes away the .xml part?


Thanks.
yes, output with method text


<xsl:value-of select="recipe/@href"/>


best regards

Arbitrator
11-02-2009, 05:01 AM
Is it possible to use XSL so that it only returns the "Store_Recipe__Polenta" part, and takes away the .xml part?Yes:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="application/xml" href="CF180233.xslt" media="all"?>
<recipe href="Store_Recipe__Polenta.xml"></recipe>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform">
<xslt:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" standalone="yes" indent="yes" media-type="application/xml"></xslt:output>
<xslt:template match="child::recipe">
<recipe>
<xslt:value-of select="substring(attribute::href, 0, string-length(attribute::href) - 3)"></xslt:value-of>
</recipe>
</xslt:template>
</xslt:stylesheet>

oesxyl
11-02-2009, 08:21 AM
Yes:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="application/xml" href="CF180233.xslt" media="all"?>
<recipe href="Store_Recipe__Polenta.xml"></recipe>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform">
<xslt:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" standalone="yes" indent="yes" media-type="application/xml"></xslt:output>
<xslt:template match="child::recipe">
<recipe>
<xslt:value-of select="substring(attribute::href, 0, string-length(attribute::href) - 3)"></xslt:value-of>
</recipe>
</xslt:template>
</xslt:stylesheet>
or another way:

<xsl:value-of select="substring-before(@href,'.xml')"/>


this is a good resource for functions/operators in xslt:

http://zvon.org/xxl/XSLTreference/Output/index.html

thank you, I miss the part with removing extension. :)

best regards

Arbitrator
11-02-2009, 08:48 AM
or another way:

<xsl:value-of select="substring-before(@href,'.xml')"/>
This is a better way. Unfortunately, I didn't read that far into the XPath 1.0 spec so I missed it.

this is a good resource for functions/operators in xslt:

http://zvon.org/xxl/XSLTreference/Output/index.htmlI've been to that site before and don't like it.

For one, it's hard to navigate; I didn't even notice there were sections for "Functions", "Axes", and "Operators" until just now since the site does not have a clearly understandable navigation system.

For two, the examples are not written in an easy-to-read manner; they use singular letters for variable names like $A and label examples only with meaningless numbers.

Finally, it doesn't *explain* anything in a new way; rather, it just quotes the spec. I may as well just read the spec instead of visiting this site.

oesxyl
11-02-2009, 09:19 AM
This is a better way. Unfortunately, I didn't read that far into the XPath 1.0 spec so I missed it.

I've been to that site before and don't like it.

For one, it's hard to navigate; I didn't even notice there were sections for "Functions", "Axes", and "Operators" until just now since the site does not have a clearly understandable navigation system.

For two, the examples are not written in an easy-to-read manner; they use singular letters for variable names like $A and label examples only with meaningless numbers.

Finally, it doesn't *explain* anything in a new way; rather, it just quotes the spec. I may as well just read the spec instead of visiting this site.
I couldn't say that I like it very much but is the best you can get from net now, :) and yes you are right about content, is extracted from specification, in another word is another view of the specs and from that point of view if navigation is hard for you or don't like it's normal to be useless. :)
About variables names I don't know if another names could be better or more confusing, in fact they don't do a specific task to have such intuitive names.

best regards