Xml XSLT-使用正则表达式和字符串长度查找属性值
输入XML就像Xml XSLT-使用正则表达式和字符串长度查找属性值,xml,xslt,Xml,Xslt,输入XML就像 <link idref="c001_r1" target="page">1</link> <link idref="r164">2004</link> <link idref="ref1">Austen et al. 1975</link> 产出应该是, <link idref="c001_r1" target="literature"><sup>1</sup></
<link idref="c001_r1" target="page">1</link>
<link idref="r164">2004</link>
<link idref="ref1">Austen et al. 1975</link>
产出应该是,
<link idref="c001_r1" target="literature"><sup>1</sup></link>
<link idref="r164" target="literature">2004</link>
<link idref="ref1" target="literature">Austen et al. 1975</link>
我们编写了xslt,如下所示
<xsl:template match="link">
<xsl:choose>
<xsl:when test="starts-with(@idref, 'r') and string-length(.) = 4">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="target">
<xsl:value-of select="'literature'" />
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:when>
<xsl:when test="starts-with(@idref, 'ref') and string-length(.) > 4">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="target">
<xsl:value-of select="'literature'" />
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:when>
<xsl:when test="contains(@idref, '(.+)_r')">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="target">
<xsl:value-of select="'literature'" />
</xsl:attribute>
<sup>
<xsl:apply-templates select="node()"/></sup>
</xsl:copy>
</xsl:when>
<xsl:when test="starts-with(@idref, 'ref') and string-length(.) < 3">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="target">
<xsl:value-of select="'literature'" />
</xsl:attribute>
<sup>
<xsl:apply-templates select="node()"/>
</sup>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="target"><xsl:text>page</xsl:text>
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
使用上述xslt时,第3和第4个条件不起作用。我们需要根据属性值和字符串长度添加属性。我们不能少提而不是说它不起作用,你应该真正提到你所遇到的任何错误。感谢您的宝贵回复,并进一步澄清。这真的很有帮助。
<xsl:when test="starts-with(@idref, 'ref') and string-length(.) < 3">
<xsl:when test="starts-with(@idref, 'ref') and string-length(.) lt 3">
<xsl:when test="matches(@idref, '.+_r.+')">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="link">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:if test="(starts-with(@idref, 'r') and string-length(.) = 4)
or (starts-with(@idref, 'ref') and string-length(.) > 4)
or (matches(@idref, '.+_r.+'))
or (starts-with(@idref, 'ref') and string-length(.) lt 3)">
<xsl:attribute name="target" select="'literature'" />
</xsl:if>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>