使用XSLT将一个XML文件转换为另一个XML文件

时间:2018-04-11 21:59:01

标签: xml xslt xslt-1.0

问题:如何使用XSLT将一种XML文件格式转换为另一种XML文件格式。我是xml / xslt的新手,所以我需要帮助。我需要在xml中吐出一个字符串。

XML1

<?xml version="1.0" encoding="utf-8"?>
<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>

XML2

<?xml version="1.0" encoding="utf-8"?>
<root type="array">
<persons>
<person_id>_:genid1</person_id>
<type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
<oneofs>
<oneof>This is </oneof>
<oneofagain>a very long string</oneofagain>
</oneofs>
</persons>
<persons>
<person_id>_:genid108</person_id>
<type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
<oneofs>
<oneof>This is </oneof>
<oneofagain>another very long string</oneofagain>
</oneofs>
</persons>
</root>

1 个答案:

答案 0 :(得分:1)

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

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>

XSLT 1.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 </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>

看看上面的内容,让我知道什么是没有意义的。

小提琴:http://xsltfiddle.liberty-development.net/gWmuiHV