Xslt 如何在定义的点插入xml节点集

Xslt 如何在定义的点插入xml节点集,xslt,xslt-1.0,Xslt,Xslt 1.0,xslt有函数(如子字符串,反之亦然)或如何解决它?我有xml: <document> <Line> <Line-Item> <LineNumber>10</LineNumber> <EAN>111</EAN> <BIC>123123</BIC> <SIC>AVD091</SIC> &l

xslt有函数(如子字符串,反之亦然)或如何解决它?我有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>

字段行号从1列位置开始,EAN从11列位置开始,BIC从19开始,SIC从31开始

这里是一个示例样式表(很抱歉,XSLT 2.0在您的评论指示请求1.0之前就开始编写):





这是一个示例样式表(很抱歉,XSLT 2.0是在您的评论指示请求1.0之前开始编写的):





试试这个XSLT 1.0样式表。pad模板是Martin的mf:pad函数的xslt1.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>

试试这个XSLT 1.0样式表。pad模板是Martin的mf:pad函数的xslt1.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>

这个简短的通用转换:

<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>
注意事项

<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>

元素
my:fields
可以放在自己的XML文档中。因此,如果需要修改某些字段的宽度,则不需要修改XSLT代码。

此简短的通用转换:

<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>
注意事项

<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>

元素
my:fields
可以放在自己的XML文档中。因此,如果某些字段的宽度需要修改,则不需要对XSLT代码进行任何修改。

谢谢您的回答,我将使用它。谢谢您的回答,我将使用它。@Petras:欢迎您。请考虑接受更短更通用的解决方案。请考虑接受较短且更通用的解决方案。