English 中文(简体)
我如何利用XSLT 1.0来为简单的文字产出提供正当理由?
原标题:How can I use XSLT 1.0 to right justify plain text output?
  • 时间:2012-01-13 22:33:29
  •  标签:
  • xslt

I m working with an XML file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="align-test.xsl"?>
<alignTest>
    <item name="Some Name" number="3"></item>
    <item name="Another Name" number="10"></item>
    <item name="A Third One" number="43"></item>
    <item name="A Really Long Name" number="100"></item>
</alignTest>

我的目标是产生一份内容简明的文字文件,在其中填充格式。 一、如何调整和打下案文栏,以及使用这一风格表格的单独栏目:

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

        <xsl:for-each select="alignTest/item">
            <!-- Scroll right. The next line keeps going, but might not look like it due to formatting -->
            <xsl:value-of select="substring(concat(@name,                            ), 1, 22)"/>
            <xsl:text> | </xsl:text>
            <xsl:value-of select="@number"/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>

哪些产出:

Some Name              | 3
Another Name           | 10
A Third One            | 43
A Really Long Name     | 100

我也认为数字价值是正确的。 与此类似:

Some Name              |   3
Another Name           |  10
A Third One            |  43
A Really Long Name     | 100

我如何利用XSLT 1.0来正确解释这种明文案文?

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 下面是你回答的更可读和更有效的版本:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="item">
        <xsl:call-template name="padRightSide">
            <xsl:with-param name="stringToPad" select="@name"/>
            <xsl:with-param name="totalLength" select="22"/>
        </xsl:call-template>

        <xsl:text>|</xsl:text>

        <xsl:call-template name="padLeftSide">
            <xsl:with-param name="stringToPad" select="@number"/>
            <xsl:with-param name="totalLength" select="5"/>
        </xsl:call-template>

        <xsl:text>&#10;</xsl:text>
 </xsl:template>

 <!-- template to pad the left side of strings (and right justificy) -->
 <xsl:template name="padLeftSide">
    <xsl:param name="stringToPad"/>
    <xsl:param name="totalLength"/>
    <xsl:param name="padChar" select="   "/>
    <xsl:param name="padBuffer" select=
    "concat($padChar,$padChar,$padChar,$padChar,$padChar,
            $padChar,$padChar,$padChar,$padChar,$padChar
            )"/>
    <xsl:variable name="vNewString" select=
                       "concat($padBuffer, $stringToPad)"/>

    <xsl:choose>
        <xsl:when test="not(string-length($vNewString) >= $totalLength)">
            <xsl:call-template name="padLeftSide">
                <xsl:with-param name="stringToPad" select="$vNewString"/>
                <xsl:with-param name="totalLength" select="$totalLength"/>
                <xsl:with-param name="padChar" select="$padChar"/>
                <xsl:with-param name="padBuffer" select="$padBuffer"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select=
            "substring($vNewString, 
                       string-length($vNewString) - $totalLength + 1)"/>
        </xsl:otherwise>
    </xsl:choose>
 </xsl:template>

 <!-- template to pad the right side of strings -->
 <xsl:template name="padRightSide">
    <xsl:param name="totalLength"/>
    <xsl:param name="padChar" select="   "/>
    <xsl:param name="stringToPad"/>
    <xsl:param name="padBuffer" select=
    "concat($padChar,$padChar,$padChar,$padChar,$padChar,
            $padChar,$padChar,$padChar,$padChar,$padChar
            )"/>
    <xsl:variable name="vNewString" select=
                       "concat($stringToPad, $padBuffer)"/>
    <xsl:choose>
        <xsl:when test="not(string-length($vNewString) >= $totalLength)">
            <xsl:call-template name="padRightSide">
                <xsl:with-param name="stringToPad" select="$vNewString"/>
                <xsl:with-param name="totalLength" select="$totalLength"/>
                <xsl:with-param name="padChar" select="$padChar"/>
                <xsl:with-param name="padBuffer" select="$padBuffer"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring($vNewString,1,$totalLength)"/>
        </xsl:otherwise>
    </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

www.un.org/Depts/DGACM/index_spanish.htm 另一项改进是动态计算最大增量,不必人工计算。

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vNamesMaxLen">
  <xsl:call-template name="maxLength">
   <xsl:with-param name="pNodes"
                   select="/*/item/@name"/>
  </xsl:call-template>
 </xsl:variable>


 <xsl:variable name="vNumsMaxLen">
  <xsl:call-template name="maxLength">
   <xsl:with-param name="pNodes"
                   select="/*/item/@number"/>
  </xsl:call-template>
 </xsl:variable>

 <xsl:template match="item">
        <xsl:call-template name="padRightSide">
            <xsl:with-param name="stringToPad"
                            select="@name"/>
            <xsl:with-param name="totalLength"
                            select="$vNamesMaxLen+1"/>
        </xsl:call-template>

        <xsl:text>|</xsl:text>

        <xsl:call-template name="padLeftSide">
            <xsl:with-param name="stringToPad"
                            select="@number"/>
            <xsl:with-param name="totalLength"
                            select="$vNumsMaxLen+1"/>
        </xsl:call-template>

        <xsl:text>&#10;</xsl:text>
 </xsl:template>

 <xsl:template name="maxLength">
  <xsl:param name="pNodes" select="/.."/>

  <xsl:for-each select="$pNodes">
   <xsl:sort select="string-length()" data-type="number"
             order="descending"/>
    <xsl:if test="position() = 1">
     <xsl:value-of select="string-length()"/>
    </xsl:if>
  </xsl:for-each>
 </xsl:template>

 <!-- template to pad the left side of strings (and right justificy) -->
 <xsl:template name="padLeftSide">
    <xsl:param name="stringToPad"/>
    <xsl:param name="totalLength"/>
    <xsl:param name="padChar" select="   "/>
    <xsl:param name="padBuffer" select=
    "concat($padChar,$padChar,$padChar,$padChar,$padChar,
            $padChar,$padChar,$padChar,$padChar,$padChar
            )"/>
    <xsl:variable name="vNewString" select=
                    "concat($padBuffer, $stringToPad)"/>

    <xsl:choose>
        <xsl:when test="not(string-length($vNewString) >= $totalLength)">
            <xsl:call-template name="padLeftSide">
                <xsl:with-param name="stringToPad" select="$vNewString"/>
                <xsl:with-param name="totalLength" select="$totalLength"/>
                <xsl:with-param name="padChar" select="$padChar"/>
                <xsl:with-param name="padBuffer" select="$padBuffer"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select=
            "substring($vNewString, 
                       string-length($vNewString) - $totalLength + 1)"/>
        </xsl:otherwise>
    </xsl:choose>
 </xsl:template>

 <!-- template to pad the right side of strings -->
 <xsl:template name="padRightSide">
    <xsl:param name="totalLength"/>
    <xsl:param name="padChar" select="   "/>
    <xsl:param name="stringToPad"/>
    <xsl:param name="padBuffer" select=
    "concat($padChar,$padChar,$padChar,$padChar,$padChar,
            $padChar,$padChar,$padChar,$padChar,$padChar
            )"/>
    <xsl:variable name="vNewString" select=
                    "concat($stringToPad, $padBuffer)"/>
    <xsl:choose>
        <xsl:when test="not(string-length($vNewString) >= $totalLength)">
            <xsl:call-template name="padRightSide">
                <xsl:with-param name="stringToPad" select="$vNewString"/>
                <xsl:with-param name="totalLength" select="$totalLength"/>
                <xsl:with-param name="padChar" select="$padChar"/>
                <xsl:with-param name="padBuffer" select="$padBuffer"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring($vNewString,1,$totalLength)"/>
        </xsl:otherwise>
    </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
问题回答

我在

所描述的简单方式是:

<xsl:value-of select="substring(concat(      , @number), string-length(@number) + 1, 4)"/>

该网页还列出了在左边和右边做dding的两个模板。 他们自行休养,并拿出适当数额。 (请注意,如果请求的长度少于正在施工的扼杀期限,也将予以缩减。) 模板还不包括改变用于婚礼的特性。

它们需要更多的法典来执行,但可能比较容易维持。 The following s a edition of the original Formatsheet updated with the two model to show their use:

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

    <xsl:template match="/">
        <xsl:for-each select="alignTest/item">

            <xsl:call-template name="padRightSide">
                <xsl:with-param name="stringToPad" select="@name"></xsl:with-param>
                <xsl:with-param name="totalLength" select="22"></xsl:with-param>
                <xsl:with-param name="padCharacter" select="   "></xsl:with-param>
            </xsl:call-template>

            <xsl:text>|</xsl:text>

            <xsl:call-template name="padLeftSide">
                <xsl:with-param name="stringToPad" select="@number"/>
                <xsl:with-param name="totalLength" select="5"/>
                <xsl:with-param name="padCharacteracter" select="   "/>
            </xsl:call-template>

            <xsl:text>&#10;</xsl:text>

        </xsl:for-each>
    </xsl:template>

    <!-- template to pad the left side of strings (and right justificy) -->
    <xsl:template name="padLeftSide">
        <xsl:param name="stringToPad"/>
        <xsl:param name="totalLength"/>
        <xsl:param name="padCharacteracter"/>

        <xsl:choose>
            <xsl:when test="string-length($stringToPad) &lt; $totalLength">
                <xsl:call-template name="padLeftSide">
                    <xsl:with-param name="stringToPad" select="concat($padCharacteracter,$stringToPad)"/>
                    <xsl:with-param name="totalLength" select="$totalLength"/>
                    <xsl:with-param name="padCharacteracter" select="$padCharacteracter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring($stringToPad,string-length($stringToPad) - $totalLength + 1)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- template to pad the right side of strings -->
    <xsl:template name="padRightSide">
        <xsl:param name="padCharacter"/> 
        <xsl:param name="stringToPad"/>
        <xsl:param name="totalLength"/>
        <xsl:choose>
            <xsl:when test="string-length($stringToPad) &lt; $totalLength">
                <xsl:call-template name="padRightSide">
                    <xsl:with-param name="padCharacter" select="$padCharacter"/>
                    <xsl:with-param name="stringToPad" select="concat($stringToPad,$padCharacter)"/>
                    <xsl:with-param name="totalLength" select="$totalLength"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring($stringToPad,1,$totalLength)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

当然,你可以减少其足迹的大小,少之又少地 co穿。





相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签