我在这里找到了一些代码,这是一个不错的开始,但它与我正在寻找的有点不同,我不确定如何继续。我想要做的是复制整个 XML 文件,通过基于多个分隔符拆分 Author 元素来创建新元素,并摆脱原始的“Author”元素名称。每个作者“集”由分号分隔,并以回车或仅回车结束,如记录 4。任何帮助将不胜感激。我是 XSLT 新手,对提供的解决方案的任何解释都会很棒,因为我想了解解决方案而不仅仅是让它工作。谢谢。
编码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:e="http://localhost">
<xsl:output indent="yes"/>
<e:e>AuthorName</e:e>
<e:e>AuthorLocation</e:e>
<e:e>AuthorPhone</e:e>
<xsl:variable name="vElement" select="document('')/*/e:*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Author/text()" name="tokenizer">
<xsl:param name="pString" select="string()"/>
<xsl:param name="pPosition" select="1"/>
<xsl:if test="$pString">
<xsl:element name="{$vElement[$pPosition]}">
<xsl:value-of select="normalize-space(substring-before(concat($pString,';'),';'))"/>
</xsl:element>
<xsl:call-template name="tokenizer">
<xsl:with-param name="pString" select="substring-after($pString,';')"/>
<xsl:with-param name="pPosition" select="$pPosition + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
XML 文件
<RECORD_SET>
<RECORD NUM="1">
<Title>Title1</Title>
<Author>Author Name 1; Author Location 1; Author Phone Number 1</Author>
<Date>11/20/1976</Date>
</RECORD>
<RECORD NUM="2">
<Title>Title2</Title>
<Author>Author Name 2; Author Location 2; Author Phone Number 2
Author Name 3; Author Location 3; Author Phone Number 3
Author Name 4; Author Location 4; Author Phone Number 4</Author>
<Date>10/20/2001</Date>
</RECORD>
<RECORD NUM="3">
<Title>Title3</Title>
<Author>Author Name 5; Author Location 5; Author Phone Number 5
Author Name 6; Author Location 6; Author Phone Number 6</Author>
<Date>09/18/1966</Date>
</RECORD>
<RECORD NUM="4">
<Title>Title4</Title>
<Author>Author Name 7
Author Name 8</Author>
<Date>01/18/1956</Date>
</RECORD>
</RECORD_SET>
期望的输出
<RECORD_SET>
<RECORD NUM="1">
<Title>Title1</Title>
<AuthorName>Author Name 1</Author>
<AuthorLocation>Author Location 1</AuthorLocation>
<AuthorPhone>Author Phone Number 1</AuthorPhone>
<Date>11/20/1976</Date>
</RECORD>
<RECORD NUM="2">
<Title>Title2</Title>
<AuthorName>Author Name 2</Author>
<AuthorLocation>Author Location 2</AuthorLocation>
<AuthorPhone>Author Phone Number 2</AuthorPhone>
<AuthorName>Author Name 3</Author>
<AuthorLocation>Author Location 3</AuthorLocation>
<AuthorPhone>Author Phone Number 3</AuthorPhone>
<AuthorName>Author Name 4</Author>
<AuthorLocation>Author Location 4</AuthorLocation>
<AuthorPhone>Author Phone Number 4</AuthorPhone>
<Date>10/20/2001</Date>
</RECORD>
<RECORD NUM="3">
<Title>Title3</Title>
<AuthorName>Author Name 5</Author>
<AuthorLocation>Author Location 5</AuthorLocation>
<AuthorPhone>Author Phone Number 5</AuthorPhone>
<AuthorName>Author Name 6</Author>
<AuthorLocation>Author Location 6</AuthorLocation>
<AuthorPhone>Author Phone Number 6</AuthorPhone>
<Date>09/18/1966</Date>
</RECORD>
<RECORD NUM="4">
<Title>Title4</Title>
<AuthorName>Author Name 7</AuthorName>
<AuthorName>Author Name 8</AuthorName>
<Date>01/18/1956</Date>
</RECORD>
</RECORD_SET>
电流输出
<RECORD NUM="1">
<Title>Title1</Title>
<Author>
<AuthorName>Author Name 1</AuthorName>
<AuthorLocation>Author Location 1</AuthorLocation>
<AuthorPhone>Author Phone Number 1</AuthorPhone>
</Author>
<Date>11/20/1976</Date>
</RECORD>
<RECORD NUM="2">
<Title>Title2</Title>
<Author>
<AuthorName>Author Name 2</AuthorName>
<AuthorLocation>Author Location 2</AuthorLocation>
<AuthorPhone>Author Phone Number 2 Author Name 3</AuthorPhone>Author Location 3Author Phone Number 3 Author Name 4Author Location 4Author Phone Number 4
</Author>
<Date>10/20/2001</Date>
</RECORD>
<RECORD NUM="3">
<Title>Title3</Title>
<Author>
<AuthorName>Author Name 5</AuthorName>
<AuthorLocation>Author Location 5</AuthorLocation>
<AuthorPhone>Author Phone Number 5 Author Name 6</AuthorPhone>Author Location 6Author Phone Number 6
</Author>
<Date>09/18/1966</Date>
</RECORD>
<RECORD NUM="4">
<Title>Title4</Title>
<Author>
<AuthorName>Author Name 7 Author Name 8</AuthorName>
</Author>
<Date>01/18/1956</Date>
</RECORD>