Xml 计算XSLT中的行数

Xml 计算XSLT中的行数,xml,xslt,Xml,Xslt,转换此XML时,我需要计算此XML节点中的换行数 <SpecialInstraction>One Two Three Four Five Six Seven Eight Nine Ten Eleven twelve thirteen fourteen fifteen</SpecialInstraction> 1 两个 三 四 五 六 七 八 九 十 十一 十二 十三 十四 十五 像上面的例子一样,有14个换行符 我试图确定“&&xD;&&xA;”和“&&13;

转换此XML时,我需要计算此XML节点中的换行数

    <SpecialInstraction>One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Eleven
twelve
thirteen
fourteen
fifteen</SpecialInstraction>
1
两个
三
四
五
六
七
八
九
十
十一
十二
十三
十四
十五
像上面的例子一样,有14个换行符

我试图确定“
&&xD;&&xA;
”和“
&&13;
”的出现次数,但即使上述XML没有变化,每次该XML进行序列化/反序列化时,出现次数都会变化


有人知道如何从XSLT中XML节点的值中获得行数吗

在计算字符串中的空格数方面也有类似的问题。我已经修改了从那里偷来的代码来使用换行符

<xsl:template name="word-count">
        <xsl:param name="data"/>
        <xsl:param name="num"/>
        <xsl:variable name="newdata" select="$data"/>
        <xsl:variable name="remaining" select="substring-after($newdata,'&#10;')"/>                

        <xsl:choose>
                <xsl:when test="$remaining">
                        <xsl:call-template name="word-count">
                                <xsl:with-param name="data" select="$remaining"/>
                                <xsl:with-param name="num" select="$num+1"/>
                        </xsl:call-template>
                </xsl:when>
                <xsl:when test="$num = 1">
                        no words...
                </xsl:when>
                <xsl:otherwise>
                        <xsl:value-of select="$num"/>
                </xsl:otherwise>
        </xsl:choose>                
</xsl:template>

没有语言。。。

在计算字符串中的空格数时,也有一个类似的问题。我已经修改了从那里偷来的代码来使用换行符

<xsl:template name="word-count">
        <xsl:param name="data"/>
        <xsl:param name="num"/>
        <xsl:variable name="newdata" select="$data"/>
        <xsl:variable name="remaining" select="substring-after($newdata,'&#10;')"/>                

        <xsl:choose>
                <xsl:when test="$remaining">
                        <xsl:call-template name="word-count">
                                <xsl:with-param name="data" select="$remaining"/>
                                <xsl:with-param name="num" select="$num+1"/>
                        </xsl:call-template>
                </xsl:when>
                <xsl:when test="$num = 1">
                        no words...
                </xsl:when>
                <xsl:otherwise>
                        <xsl:value-of select="$num"/>
                </xsl:otherwise>
        </xsl:choose>                
</xsl:template>

没有语言。。。
使用

string-length() - string-length(translate(., '&#xA;', ''))
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text()">
     <xsl:value-of select=
     "string-length() - string-length(translate(., '&#xA;', ''))"/>
 </xsl:template>
</xsl:stylesheet>
<SpecialInstraction>One
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight
    Nine
    Ten
    Eleven
    twelve
    thirteen
    fourteen
    fifteen</SpecialInstraction>
虽然通过使用上述XPath一行程序,您根本不需要XSLT,但为了完整起见,这里就是它了。

string-length() - string-length(translate(., '&#xA;', ''))
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text()">
     <xsl:value-of select=
     "string-length() - string-length(translate(., '&#xA;', ''))"/>
 </xsl:template>
</xsl:stylesheet>
<SpecialInstraction>One
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight
    Nine
    Ten
    Eleven
    twelve
    thirteen
    fourteen
    fifteen</SpecialInstraction>
注意事项

string-length() - string-length(translate(., '&#xA;', ''))
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text()">
     <xsl:value-of select=
     "string-length() - string-length(translate(., '&#xA;', ''))"/>
 </xsl:template>
</xsl:stylesheet>
<SpecialInstraction>One
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight
    Nine
    Ten
    Eleven
    twelve
    thirteen
    fourteen
    fifteen</SpecialInstraction>
此解决方案仅计算NL字符数。为什么我忽略了CRs?因为根据,任何兼容的XML解析器都必须执行以下操作:

在执行任何其他处理之前,XML文档中实际存在的所有#xD字符要么被删除,要么被#xA字符替换 完成了

使用

string-length() - string-length(translate(., '&#xA;', ''))
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text()">
     <xsl:value-of select=
     "string-length() - string-length(translate(., '&#xA;', ''))"/>
 </xsl:template>
</xsl:stylesheet>
<SpecialInstraction>One
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight
    Nine
    Ten
    Eleven
    twelve
    thirteen
    fourteen
    fifteen</SpecialInstraction>
虽然通过使用上述XPath一行程序,您根本不需要XSLT,但为了完整起见,这里就是它了。

string-length() - string-length(translate(., '&#xA;', ''))
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text()">
     <xsl:value-of select=
     "string-length() - string-length(translate(., '&#xA;', ''))"/>
 </xsl:template>
</xsl:stylesheet>
<SpecialInstraction>One
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight
    Nine
    Ten
    Eleven
    twelve
    thirteen
    fourteen
    fifteen</SpecialInstraction>
注意事项

string-length() - string-length(translate(., '&#xA;', ''))
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text()">
     <xsl:value-of select=
     "string-length() - string-length(translate(., '&#xA;', ''))"/>
 </xsl:template>
</xsl:stylesheet>
<SpecialInstraction>One
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight
    Nine
    Ten
    Eleven
    twelve
    thirteen
    fourteen
    fifteen</SpecialInstraction>
此解决方案仅计算NL字符数。为什么我忽略了CRs?因为根据,任何兼容的XML解析器都必须执行以下操作:

在执行任何其他处理之前,XML文档中实际存在的所有#xD字符要么被删除,要么被#xA字符替换 完成了