XSL - 如何设置整个输出文本文件的最大长度

时间:2016-09-22 13:10:10

标签: xslt xsl-fo

<xsl:output method="text" xmlns:saxon="http://saxon.sf.net/" saxon:line-length="1000"/>

我尝试了类似的东西,但它不起作用

eXAMPLE :) 假设我有xml

<?xml version="1.0" encoding="UTF-8"?>
<LeveL Plan="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">


<space2000>
<values>
<value jak="77" rak="12"</value>
<value jak="66" rak="345"></value>
<value jak="-50" rak="67"></value>
<value jak="But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" rak="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?">1</value>
</values> 

</space2000>


</LeveL>

我想做一些像

这样的操作
<xsl:stylesheet version="2.0" 
xmlns:fn ="http://www.w3.org/2005/xpath-functions"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:output method="text" xmlns:saxon="http://saxon.sf.net/" saxon:line-length="1000"/>

<xsl:template match="/">
<xsl:for-each select="(LeveL/space2000/values)">
<xsl:variable name="JJJ"  select="(value/@jak"/>
<xsl:variable name="RRR"  select="(value/@rak)"/>
<xsl:text>
O
</xsl:text>
<xsl:text>LA</xsl:text>
<xsl:value-of select="$JJJ"></xsl:value-of>
<xsl:text> </xsl:text><xsl:text>LO</xsl:text>
<xsl:value-of select="$RRR"></xsl:value-of>
<xsl:text>
O
</xsl:text>

<xsl:choose>
     <xsl:when test="( $JJJ gt 0)">
        <xsl:value-of select="$JJJ"/>
     </xsl:when>
     <xsl:when test="( $JJJ lt 0)">
        <xsl:value-of select="$RRR"/>
     </xsl:when>
       <xsl:when test="( $RRR gt 0)">
        <xsl:value-of select="fn:concat($JJJ,$RRR)"/>
     </xsl:when>
     <xsl:when test="( $RRR lt 0)">
        <xsl:value-of select="00000000000000"/>
     </xsl:when>   

</xsl:choose>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>
  

我希望输出的文字不超过1000个标志   包括空格,它应该只是在1000个标志后被提取

1 个答案:

答案 0 :(得分:1)

你的问题不明确,这个例子特别令人困惑。

如果我正确猜测,您希望逐步连接某些节点的字符串值,直到达到某个预定义长度的字符串。以下示例显示了一个方法:

<强> XML

<root>
    <item>Not responsible for direct, indirect, incidental or consequential damages resulting from any defect, error or failure to perform.</item>
    <item>This news release contains forward-looking statements that involve risks, uncertainties and assumptions.</item>
    <item>If such risks or uncertainties materialize or such assumptions prove incorrect, the results of this organization and its consolidated subsidiaries could differ materially from those expressed or implied by such forward-looking statements and assumptions.</item>
    <item>All statements other than statements of historical fact are statements that could be deemed forward-looking statements, including but not limited to statements of the plans, strategies and objectives of management for future operations;</item>
    <item>any statements concerning expected development, performance or market share relating to products and services;</item>
    <item>anticipated operational and financial results; any statements of expectation or belief; and any statements of assumptions underlying any of the foregoing.</item>
    <item>Risks, uncertainties and assumptions include the achievement of expected results and other risks that are described from time to time in quarterly reports.</item>
    <item>This organization assumes no obligation and does not intend to update these forward-looking statements.</item>
</root>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />

<xsl:template match="/root">
    <xsl:call-template name="get-text">
        <xsl:with-param name="items" select="item"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="get-text">
    <xsl:param name="items" select="/.."/>
    <xsl:param name="text"/>
    <xsl:param name="max-length" select="300"/>
    <xsl:param name="prefix">BEGIN ITEM:&#10;</xsl:param>
    <xsl:param name="suffix">&#10;END ITEM&#10;&#10;</xsl:param>
    <xsl:variable name="next-text">
        <xsl:value-of select="$text"/>
        <xsl:value-of select="$prefix"/>
        <xsl:value-of select="$items[1]"/>
        <xsl:value-of select="$suffix"/>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="string-length($next-text) &lt; $max-length">
            <!-- recursive call -->
            <xsl:call-template name="get-text">
                <xsl:with-param name="items" select="$items[position() > 1]"/>
                <xsl:with-param name="text" select="$next-text"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring($next-text, 1, $max-length)"/>
        </xsl:otherwise>
    </xsl:choose>   
</xsl:template>

</xsl:stylesheet>

<强>结果

BEGIN ITEM:
Not responsible for direct, indirect, incidental or consequential damages resulting from any defect, error or failure to perform.
END ITEM

BEGIN ITEM:
This news release contains forward-looking statements that involve risks, uncertainties and assumptions.
END ITEM

BEGIN ITEM:
If such r

在此示例中,结果恰好包含300个字符。

如果使用XSLT 2.0,则可以使用函数替换递归命名模板。