开发者

XSLT - activate xpath function in XSLT

开发者 https://www.devze.com 2022-12-08 20:55 出处:网络
i have the following xml, which is the result of runnig xslt: <?xml version=\"1.0\" encoding=\"UTF-8\"?>

i have the following xml, which is the result of runnig xslt:

<?xml version="1.0" encoding="UTF-8"?>
<toc xmlns:fn="http://www.w3.org/2004/07/xpath-functions" label="Sample Table of Contents">
    <topic label="Title1" href="Ref1#ref1">
        <topic label="Title 2" href="Ref2#ref2">
            <topic label="Title3" href="Ref3#ref3"/>
            <topic label="Title4" href="Ref4#ref4"/>
        </topic>
        <topic label="Title5" href="Ref5#ref5"/>
    </topic>
    <topic label="Title6" href="Ref6#ref6"/>
</toc>

and the following XSLT that produces this xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="D:\Documents and Settings\oshecht\Desktop\XSL\Copy of toc.xml"?>
<?altova_samplexml D:\Documents and Settings\oshecht\Desktop\XSL\Copy of toc.xml?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2004/07/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="BODY">
        <toc label="Sample Table of Contents">
            <xsl:apply-templates select="UL/LI/OBJECT"/>
        </toc>
    </xsl:template>
    <xsl:template match="OBJECT">
        <topic label="{param[@name='Name']/@value}"开发者_运维百科 href="{param[@name='Local']/@value}">
            <xsl:apply-templates select="following-sibling::UL/LI/OBJECT"/>
        </topic>
    </xsl:template>
</xsl:stylesheet>

i want that in the output xml i will have in :

the follwing:

- the same line but instead of href="Ref1#ref1" to have:

href="Ref1" - eliminate everything after "#"

i know about the function substring-before(Ref1#ref1,'#') but how can i activate it from my XSLT?

can you please advise?


You could wrap the XPATH expression selecting the @value inside the attribute value template for the @href, which will yield the result of the function call for the @href value:

<xsl:template match="OBJECT">
    <topic label="{param[@name='Name']/@value}" href="{substring-before(param[@name='Local']/@value, '#')}">
            <xsl:apply-templates select="following-sibling::UL/LI/OBJECT"/>
    </topic>
</xsl:template>

If the attribute value template is confusing, you could also break out the @href attribute this way:

<xsl:template match="OBJECT">
    <topic label="{param[@name='Name']/@value}">
            <xsl:attribute name="href">
               <xsl:value-of select="substring-before(param[@name='Local']/@value, '#')" />
            </xsl:attribute>
            <xsl:apply-templates select="following-sibling::UL/LI/OBJECT"/>
    </topic>
</xsl:template>
0

精彩评论

暂无评论...
验证码 换一张
取 消