使用 XSLT 获取字符串中的文本
Get text in String using XSLT
使用 XSLT 获取字符串中的文本
输入:
<chapter href="Sapmle_text" format="ditamap"
otherprops="navlabel(Reading) navnum(41)" class="- map/topicref bookmap/chapter ">
输出应该是:
41
尝试过 Xpath:
//xref[parent::p/following-sibling::fig]/ancestor::chapter/substring-after(@outputclass,'navnum(')
但是我试过的代码不能正常工作。如何从章节元素中获取 41
?
我正在使用 XSLT 2.0
尝试:
chapter/substring-before(substring-after(@otherprops, 'navnum('), ')')
您正在使用 DITA 并试图从 chapter/@otherprops 获取通用属性值。因此,值得开发使用 XSLT 3.0 从指定属性获取通用属性值的通用函数。 (您可以在 DITA-OT 3.x 中使用 XSLT 3.0,没有问题。)
[输入文件]
<?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>
[样式表示例]
<?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>
[输出结果]
navnum=41
使用 XSLT 获取字符串中的文本
输入:
<chapter href="Sapmle_text" format="ditamap"
otherprops="navlabel(Reading) navnum(41)" class="- map/topicref bookmap/chapter ">
输出应该是:
41
尝试过 Xpath:
//xref[parent::p/following-sibling::fig]/ancestor::chapter/substring-after(@outputclass,'navnum(')
但是我试过的代码不能正常工作。如何从章节元素中获取 41
?
我正在使用 XSLT 2.0
尝试:
chapter/substring-before(substring-after(@otherprops, 'navnum('), ')')
您正在使用 DITA 并试图从 chapter/@otherprops 获取通用属性值。因此,值得开发使用 XSLT 3.0 从指定属性获取通用属性值的通用函数。 (您可以在 DITA-OT 3.x 中使用 XSLT 3.0,没有问题。)
[输入文件]
<?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>
[样式表示例]
<?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>
[输出结果]
navnum=41