0
我正在編寫一個樣式表來創建文件中的文本字符串比允許的長度更長的報告。 (據瞭解,這是不可能與模式1.0相關的。)使用元素中指定的字符串長度來檢查另一個元素
對於每個文本,我檢查Value元素中包含的字符串的長度是否不超過Info元素中爲此指定的長度特定文本。
但是,我發現我的maxLength變量不能用作全局變量。所以即使我能夠獲得有關最大長度的信息,我也無法在我的支票中使用這些值。 我可以使用一些關於如何最好地重新設計我的樣式表的建議。
我的XSL:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<!-- Message template -->
<xsl:template match="Text" >
<xsl:variable name="messageNumber" select="@Id"/>
<xsl:variable name="maxLength"/>
<xsl:choose>
<xsl:when test="(normalize-space(Info)='')">
<xsl:variable name="maxLength" select="40" />
Message <xsl:value-of select="$messageNumber"/> is missing Max Length. Using <xsl:value-of select="$maxLength"/>
</xsl:when>
<xsl:when test="contains(Info,'char')">
<xsl:variable name="maxLength1" select="substring-before(Info, 'char')"/>
<xsl:variable name="maxLength" select="substring($maxLength1, string-length($maxLength1) - 2)" />
</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="string-length(Value) > $maxLength">
Message <xsl:value-of select="$messageNumber"/> too long: <xsl:value-of select="string-length(Value)"/> chars (max is <xsl:value-of select="$maxLength"/>)
</xsl:when>
<xsl:otherwise>
Message <xsl:value-of select="$messageNumber"/> is OK: <xsl:value-of select="string-length(Value)"/> chars (max is <xsl:value-of select="$maxLength"/>)
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我的XML:
<?xml version="1.0" ?>
<Texts>
<Text Id="1">
<Value>OK</Value>
<Info>Text length for button, max 6 chars</Info>
</Text>
<Text Id="2">
<Value>Cancel</Value>
<Info>Button, Maximum length 9 chars</Info>
</Text>
<Text Id="3">
<Value>Continue</Value>
<Info>This text cannot be longer than 14 characters. Use short form if required.</Info>
</Text>
<Text Id="4">
<Value>Twinkle, twinkle little star</Value>
<Info>Title text, Maximum length 14 chars.</Info>
</Text>
<Text Id="5">
<Value>Twinkle, twinkle little star again</Value>
<Info></Info>
</Text>
</Texts>
所需的輸出是這樣的:
Message 1 is OK: 2 chars (max is 6)
Message 2 is OK: 6 chars (max is 9)
Message 3 is OK: 8 chars (max is 14)
Message 4 too long: 28 chars (max is 14)
Message 5 is missing Max Length. Using 40
Message 5 is OK: 34 chars (max is 40)