2012-07-18 18 views
1

xslt有函數(如substring反之亦然)或者如何解決它?我的xml:如何在定義點插入xml nodet

<document> 
<Line> 
    <Line-Item> 
     <LineNumber>10</LineNumber> 
     <EAN>111</EAN> 
     <BIC>123123</BIC> 
     <SIC>AVD091</SIC> 
    </Line-Item> 
</Line> 
<Line> 
    <Line-Item> 
     <LineNumber>20</LineNumber> 
     <EAN>22222</EAN> 
     <BIC>3232332</BIC> 
     <SIC>AVD25482</SIC> 
    </Line-Item> 
</Line> 
</document> 

需要輸出:從1點的位置

10  111  123123  AVD091 
20  22222 3232332  AVD25482 

場行號開始,EAN從11列的位置,從圖19和SIC BIC起動開始從31

+0

XSLT 1.0或2.0? – 2012-07-18 11:56:36

+0

XSLT 1.0樣式表 – Petras 2012-07-18 11:57:54

回答

2

試試這個XSLT 1.0樣式表。 Pad模板是Martin的mf:pad函數的XSLT 1.0版本。

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

<xsl:template match="/"> 
    <xsl:apply-templates select="document/Line/Line-Item"/> 
</xsl:template> 

<xsl:template name="pad"> 
    <xsl:param name="value" /> 
    <xsl:param name="width" /> 
    <xsl:variable name="col-max" select="'     '"/> 
    <xsl:value-of select="substring(concat($value,$col-max), 1, $width)" /> 
</xsl:template> 

<xsl:template match="Line-Item" > 

<xsl:call-template name="pad" > 
    <xsl:with-param name="value" select="LineNumber"/> 
    <xsl:with-param name="width" select="10" /> 
</xsl:call-template> 

<xsl:call-template name="pad" > 
    <xsl:with-param name="value" select="EAN"/> 
    <xsl:with-param name="width" select="8" /> 
</xsl:call-template> 

<xsl:call-template name="pad" > 
    <xsl:with-param name="value" select="BIC"/> 
    <xsl:with-param name="width" select="12" /> 
</xsl:call-template> 

<xsl:value-of select="SIC" /> 

<xsl:value-of select="'&#x0A;'" /> 
</xsl:template> 

<xsl:template match="*" /> 
</xsl:stylesheet> 
+0

感謝您的回答我將使用它。 – Petras 2012-07-18 12:59:18

1

下面是一個樣例樣式表(XSLT 2.0,對不起,在您的評論顯示1.0請求之前開始寫入):

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:mf="http://example.com/mf" 
    exclude-result-prefixes="xs mf"> 

<xsl:param name="col-max" as="xs:string" select="'     '"/> 

<xsl:strip-space elements="*"/> 
<xsl:output method="text"/> 

<xsl:function name="mf:pad" as="xs:string"> 
    <xsl:param name="input" as="xs:string"/> 
    <xsl:param name="col-length" as="xs:integer"/> 
    <xsl:sequence select="concat($input, substring($col-max, 1, $col-length - string-length($input)))"/> 
</xsl:function> 

<xsl:template match="Line"> 
    <xsl:if test="position() > 1"> 
    <xsl:text>&#10;</xsl:text> 
    </xsl:if> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="LineNumber"> 
    <xsl:sequence select="mf:pad(., 10)"/> 
</xsl:template> 

<xsl:template match="EAN"> 
    <xsl:sequence select="mf:pad(., 9)"/> 
</xsl:template> 

<xsl:template match="BIC"> 
    <xsl:sequence select="mf:pad(., 12)"/> 
</xsl:template> 

<xsl:template match="SIC"> 
    <xsl:sequence select="mf:pad(., string-length())"/> 
</xsl:template> 

</xsl:stylesheet> 
+0

感謝您的努力。 – Petras 2012-07-18 13:04:22

2

這個簡短和通用改造

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

<my:fields> 
    <fieldset name="LineNumber" width="10"/> 
    <fieldset name="EAN" width="8"/> 
    <fieldset name="BIC" width="12"/> 
</my:fields> 

<xsl:variable name="vSpaces" select="'     '"/> 

<xsl:variable name="vFields" select="document('')/*/my:fields/*"/> 

<xsl:template match="Line-Item"> 
    <xsl:text>&#xA;</xsl:text> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="Line-Item/*"> 
    <xsl:value-of select= 
    "concat(., 
      substring($vSpaces, 
        1, 
         $vFields[@name = name(current())]/@width 
        - 
         string-length() 
        ) 
      )"/> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<document> 
    <Line> 
     <Line-Item> 
      <LineNumber>10</LineNumber> 
      <EAN>111</EAN> 
      <BIC>123123</BIC> 
      <SIC>AVD091</SIC> 
     </Line-Item> 
    </Line> 
    <Line> 
     <Line-Item> 
      <LineNumber>20</LineNumber> 
      <EAN>22222</EAN> 
      <BIC>3232332</BIC> 
      <SIC>AVD25482</SIC> 
     </Line-Item> 
    </Line> 
</document> 

產生想要的,正確的結果:

10  111  123123  AVD091 
20  22222 3232332  AVD25482 

請注意

元素my:fields可以把自己的XML文件內。因此,如果需要修改某些字段寬度,則不需要對XSLT代碼進行修改。

+0

感謝您的回答! – Petras 2012-07-18 12:59:49

+0

@Petras:不客氣。請考慮接受更短和更通用的解決方案。 – 2012-07-18 13:03:06