2009-08-06 92 views
-1

格式的文本我有以下格式的XML:XML使用XSLT

<Order> 
    <Customer> 
    <Name>kapil</name> 
    <AddressLine1>ABC</AddressLine1> 
    <PostCode>12345</postCode> 
    </Customer> 
    <Customer> 
    <Name>Soniya</name> 
    <AddressLine1>XYZPER</AddressLine1> 
    <PostCode>54321</postCode> 
    </Customer> 
    <Customer> 
    <Name>kapil</name> 
    <AddressLine1>ABC</AddressLine1> 
    <PostCode>12345</postCode> 
    </Customer> 
</Order> 

而且我想在一个特定格式的文本文件作为

Soniya XYZPER 54321 
Kapil  ABC  12345 

我想通过XSLT来做到这一点。

+4

你为什么不学习xslt ?! – 2009-08-06 11:50:41

回答

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

<xsl:template match='/Order'> 
    <xsl:for-each select='Customer'> 
    <xsl:value-of select='Name' /> 
    <xsl:value-of select='AddressLine1' /> 
    <xsl:value-of select='PostCode' /> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

这并没有解决排序(你没有指定),并获得一切都在列排队,这可能是棘手......

+0

谢谢你的帮助Ned,但... 这正是我想要做的......所有的专栏都应该正确排列......即。文本必须格式化...每个新的地址和邮政编码应该从另一个 – Kapil 2009-08-06 11:45:17

1

为了垫一个字符串在XSLT 1.0空格,你可以使用一个命名的模板是这样的:

<xsl:template name="ppad"> 
    <xsl:param name="str" /> 
    <xsl:param name="chr" select="' '" /> 
    <xsl:param name="len" select="0" /> 
    <xsl:choose> 
     <xsl:when test="string-length($str) &lt; $len"> 
      <xsl:call-template name="ppad"> 
       <xsl:with-param name="str" select="concat($str, $chr)" /> 
       <xsl:with-param name="len" select="$len" /> 
       <xsl:with-param name="chr" select="$chr" /> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$str" /> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

然后用字符串和长度作为参数调用它:

<xsl:call-template name="rpad"> 
    <xsl:with-param name="str" select="Name" /> 
    <xsl:with-param name="len" select="16" /> 
</xsl:call-template> 
+0

开始感谢吨Jorn ...但你可以ü请实施它在我的例子上面和post..i将高度赞赏...其实我是XSLT的新手:) – Kapil 2009-08-06 12:03:25

1

您可以使用XSL-FO和XSL-FO处理器,它可以帮助您格式化您的输出(在您的案例中为您的结果表格配置)并将其输出为各种格式(纯文本,PDF ...) 首先,您应该检查w3schools,并使用Apache FOP(一种开源解决方案)来处理您的XML文档。 就我而言,我使用XSL-FO从XML文件生成PDF。

+0

我不能使用XSL-FO,因为我的要求说我只需要使用XSLT – Kapil 2009-08-06 12:09:42

0

增强乔恩HORSTMANN的答案,包括LPAD和RPAD

<xsl:template name="rpad"> 
    <xsl:param name="str"/> 
    <xsl:param name="len" select="0" /> 
    <xsl:param name="chr" select="' '" /> 
    <xsl:call-template name="pad"> 
     <xsl:with-param name="str" select="$str" /> 
     <xsl:with-param name="len" select="$len" /> 
     <xsl:with-param name="chr" select="$chr" /> 
     </xsl:call-template> 
</xsl:template> 

<xsl:template name="lpad"> 
    <xsl:param name="str"/> 
    <xsl:param name="len" select="0" /> 
    <xsl:param name="chr" select="' '" /> 
    <xsl:call-template name="pad"> 
     <xsl:with-param name="str" select="$str" /> 
     <xsl:with-param name="len" select="$len" /> 
     <xsl:with-param name="chr" select="$chr" /> 
     <xsl:with-param name="justify" select="'right'" /> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="pad"> 
    <xsl:param name="str"/> 
    <xsl:param name="len" select="0" />  
    <xsl:param name="chr" select="' '" /> 
    <xsl:param name="justify" select="'left'"/> 
    <xsl:choose> 
     <xsl:when test="string-length($str) &lt; $len"> 
      <xsl:variable name="newStr"> 
       <xsl:choose> 
        <xsl:when test="$justify = 'left'"> 
         <xsl:value-of select="concat($str,' ')"/> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:value-of select="concat(' ',$str)"/> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:variable> 
      <xsl:call-template name="pad"> 
       <xsl:with-param name="len" select="$len"/> 
       <xsl:with-param name="justify" select="$justify" /> 
       <xsl:with-param name="chr" select="$chr" /> 
       <xsl:with-param name="str" select="$newStr" /> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$str"/> 
     </xsl:otherwise>    
    </xsl:choose> 
</xsl:template> 

要正确垫字符串 'myvalue的' 用空格到20个字符使用:

<xsl:call-template name="rpad"> 
    <xsl:with-param name="str" select="'myvalue'" /> 
    <xsl:with-param name="len" select="20" /> 
</xsl:call-template> 

要离开垫字符串 'myvalue的'使用空格至36个字符使用:

<xsl:call-template name="lpad"> 
    <xsl:with-param name="str" select="'myvalue'" /> 
    <xsl:with-param name="len" select="36" /> 
</xsl:call-template> 

或通话簿直接指定文本理由期望的(即,左或右)

<xsl:call-template name="pad"> 
    <xsl:with-param name="str" select="'myvalue'" /> 
    <xsl:with-param name="len" select="36" /> 
    <xsl:with-param name="justify" select="'right'" /> 
</xsl:call-template>