xsl-list
[Top] [All Lists]

[xsl] how to assign part of an attributes value to another attribute within the same element?

2008-05-29 16:52:03
Hello,
With XSL Processor: Saxon 8B and XSL v. 2.0, here is my test case:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <ptr target="abc.07.7.1fm" n=""/>
    <ptr target="abc.07.7.6tm" n=""/>
</root>

Here is my stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ptr[(_at_)target|@n]">
        <xsl:element name="ptr">
            <xsl:attribute name="target" select="@target"/>
            <xsl:attribute name="n" select="@target"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

The output I get is:

<?xml version="1.0" encoding="UTF-8"?><root>
    <ptr target="abc.07.7.1fm" n="abc.07.7.1fm"/>
    <ptr target="abc.07.7.6tm" n="abc.07.7.6tm"/>
</root>

This is not quite what I want, which is:

<?xml version="1.0" encoding="UTF-8"?><root>
    <ptr target="abc.07.7.1fm" n="1"/>
    <ptr target="abc.07.7.6tm" n="6"/>
</root>

My thought is: if I could reach into the @target attribute value, and
grab whatever numeral appears between the last period and tm or fm,
assign it as the same ptr elements @n attribute value, that would be
it.
Theoretically, I can get the number I want by matching with the regex:
abc\.07\.7\.([0-9]+)[tf]m
and replacing with $1 (in Perl syntax, which my editor supports), and
I have what I want to assign as the value of the @n attribute.
I don't know if or how XSLT can help. If you know, I thank you in
advance for sharing!
Tony

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>