xsltlife
xsltlife

Reputation: 163

Get text in String using XSLT

Get text in String using XSLT

Input :

<chapter href="Sapmle_text" format="ditamap"
otherprops="navlabel(Reading) navnum(41)" class="- map/topicref bookmap/chapter ">

Output should be:

41

Tried Xpath:

//xref[parent::p/following-sibling::fig]/ancestor::chapter/substring-after(@outputclass,'navnum(')

But my tried code is not working properly. How can I get 41 from chapter element?

I am using XSLT 2.0

Upvotes: 1

Views: 853

Answers (2)

Toshihiko Makita
Toshihiko Makita

Reputation: 1304

You are using DITA and trying to get generalized attribute value from chapter/@otherprops. So it will be worth to develop general function that gets generalized attribute value from specified attribute using XSLT 3.0. (You can use XSLT 3.0 in DITA-OT 3.x. without no problem.)

[Input File]

<?xml version="1.0" encoding="UTF-8"?>
<bookmap>
    <booktitle>
        <mainbooktitle>Test</mainbooktitle>
    </booktitle>
    <chapter href="Sapmle_text" format="ditamap"
        otherprops="navlabel(Reading) navnum(41)" class="- map/topicref bookmap/chapter "/>
</bookmap>

[Stylesheet example]

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:ahf="http://www.antennahouse.com/names/XSLT/Functions/Document"
    exclude-result-prefixes="xs math ahf"
    version="3.0">

    <xsl:template match="/">
        <xsl:variable name="navnumVal" as="xs:string?" select="ahf:getGeneralizedAttVal(/descendant::chapter[1]/@otherprops,'navnum')"/>
        <xsl:message select="'navnum=' || $navnumVal"/>
    </xsl:template>

    <!--
    function:   Get generalized form attribute value
    param:      $prmAtt: @audience, @platform, @product, or @otherprops
                $prmGeneralizedAttName: Generalized attribute name
    return:     xs:string?: Generalized attribute value
    note:       See 
                http://docs.oasis-open.org/dita/dita/v1.3/errata02/os/complete/part3-all-inclusive/archSpec/base/generalization-attributes.html#attributegeneralize
    -->
    <xsl:function name="ahf:getGeneralizedAttVal" as="xs:string?">
        <xsl:param name="prmAtt" as="attribute()"/>
        <xsl:param name="prmGeneralizedAttName" as="xs:string"/>
        <xsl:variable name="attVal" as="xs:string" select="$prmAtt => string() => normalize-space()"/>
        <xsl:variable name="regx" as="xs:string" select="$prmGeneralizedAttName || '\((.+)\)'"/>
        <xsl:analyze-string select="$attVal" regex="{$regx}">
            <xsl:matching-substring>
                <xsl:sequence select="regex-group(1)"/>
            </xsl:matching-substring>
        </xsl:analyze-string>
    </xsl:function>

</xsl:stylesheet>

[Output result]

navnum=41

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 116992

Try:

chapter/substring-before(substring-after(@otherprops, 'navnum('), ')')

Upvotes: 1

Related Questions