使用XSLT将一个XML文件转换为另一个XML文件 问题:如何使用XSLT将一种XML文件格式转换为另一种XML文件格式。我不熟悉xml/xslt,所以我需要帮助。我需要在xml中插入一个字符串。

使用XSLT将一个XML文件转换为另一个XML文件 问题:如何使用XSLT将一种XML文件格式转换为另一种XML文件格式。我不熟悉xml/xslt,所以我需要帮助。我需要在xml中插入一个字符串。,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,XML1 _:genid1 http://www.w3.org/2000/01/rdf-schema#Datatype 这是一根很长的绳子 _:genid108 http://www.w3.org/2000/01/rdf-schema#Datatype 这是另一根很长的绳子 XML2 _:genid1 http://www.w3.org/2000/01/rdf-schema#Datatype 这是 很长的绳子 _:genid108 http://www.w3.org/2000/01/rdf

XML1


_:genid1
http://www.w3.org/2000/01/rdf-schema#Datatype
这是一根很长的绳子
_:genid108
http://www.w3.org/2000/01/rdf-schema#Datatype
这是另一根很长的绳子
XML2


_:genid1
http://www.w3.org/2000/01/rdf-schema#Datatype
这是
很长的绳子
_:genid108
http://www.w3.org/2000/01/rdf-schema#Datatype
这是
另一根很长的绳子

这是一个非常广泛的问题,但也许这会有帮助

XML输入

<root type="array">
    <persons>
        <person_id>_:genid1</person_id>
        <type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
        <oneofs>
            <oneof>This is a very long string</oneof>
        </oneofs>
    </persons>
    <persons>
        <person_id>_:genid108</person_id>
        <type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
        <oneofs>
            <oneof>This is  another very long string</oneof>
        </oneofs>
    </persons>
</root>
<root type="array">
   <persons>
      <person_id>_:genid1</person_id>
      <type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
      <oneofs>
         <oneof>This is a </oneof>
         <oneof>very long </oneof>
         <oneof>string</oneof>
      </oneofs>
   </persons>
   <persons>
      <person_id>_:genid108</person_id>
      <type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
      <oneofs>
         <oneof>This is  a</oneof>
         <oneof>nother ver</oneof>
         <oneof>y long str</oneof>
         <oneof>ing</oneof>
      </oneofs>
   </persons>
</root>

_:genid1
http://www.w3.org/2000/01/rdf-schema#Datatype
这是一根很长的绳子
_:genid108
http://www.w3.org/2000/01/rdf-schema#Datatype
这是另一根很长的绳子
XSLT1.0

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

  <!--Identity template-->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="oneof">
    <xsl:call-template name="split">
      <xsl:with-param name="input" select="."/>
      <xsl:with-param name="name" select="local-name()"/>
      <xsl:with-param name="length" select="10"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="split">
    <xsl:param name="input"/>
    <xsl:param name="name"/>
    <xsl:param name="length"/>
    <xsl:variable name="remaining" select="substring($input, $length + 1)"/>
    <xsl:element name="{$name}">
      <xsl:value-of select="substring($input, 1, $length)"/>
    </xsl:element>
    <xsl:if test="$remaining">
      <xsl:call-template name="split">
        <xsl:with-param name="input" select="$remaining"/>
        <xsl:with-param name="name" select="$name"/>
        <xsl:with-param name="length" select="$length"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

输出

<root type="array">
    <persons>
        <person_id>_:genid1</person_id>
        <type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
        <oneofs>
            <oneof>This is a very long string</oneof>
        </oneofs>
    </persons>
    <persons>
        <person_id>_:genid108</person_id>
        <type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
        <oneofs>
            <oneof>This is  another very long string</oneof>
        </oneofs>
    </persons>
</root>
<root type="array">
   <persons>
      <person_id>_:genid1</person_id>
      <type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
      <oneofs>
         <oneof>This is a </oneof>
         <oneof>very long </oneof>
         <oneof>string</oneof>
      </oneofs>
   </persons>
   <persons>
      <person_id>_:genid108</person_id>
      <type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
      <oneofs>
         <oneof>This is  a</oneof>
         <oneof>nother ver</oneof>
         <oneof>y long str</oneof>
         <oneof>ing</oneof>
      </oneofs>
   </persons>
</root>

_:genid1
http://www.w3.org/2000/01/rdf-schema#Datatype
这是一个
很长
一串
_:genid108
http://www.w3.org/2000/01/rdf-schema#Datatype
这是一个
另一个
y长str
惯性导航与制导
看看上面的内容,让我知道什么是不合理的


Fiddle:

XSLT的哪个版本?如果是2.0+,请参阅发布一篇文章并展示您的尝试。版本是1.0。在这种情况下,我将从标识转换开始,添加一个命名模板,您可以传递要拆分的字符串、长度和要包装子字符串的元素名称。它应该是一个递归模板,所以它应该在需要时调用自己。您可以匹配其中一个,并从那里开始调用命名模板。试试看,然后有什么问题再回来。我该怎么做?我对xslt很陌生?请帮忙?看看用户9566024是欣赏这个慷慨的回答,还是全力帮助吸血鬼对付丹尼尔·海利,这会很有趣。:-)快到了,我真的需要第二个和第三个。需要能够更改名称。facepalm您是否尝试自己解决这个问题?提示:您可以使用param在“name”中添加“reach”。超级容易!叫它。@kjhughes-是的,你叫了!