gpt4 book ai didi

java - 使用 xslt 将文本转换为 xml

转载 作者:行者123 更新时间:2023-12-02 04:11:12 28 4
gpt4 key购买 nike

我有一个具有以下数据格式的文本文件:

FIRSTNAME
Jhon
Rembo

FIRSTNAME
James
KARL

LASTNAME
PAUL
SAM
BOND

我正在尝试使用 XSLT 2.0 将其转换为以下 XML 格式

<?xml version="1.0" encoding="UTF-8"?>
<customers>
<firstnames>
<firstname>Jhon</firstname>
<firstname>Rembo</firstname>
</firstnames>
<firstnames>
<firstname>James</firstname>
<firstname>KARL</firstname>
</firstnames>
<lastnames>
<lastname>PAUL</lastname>
<lastname>SAM</lastname>
<lastname>BOND</lastname>
</lastnames>
</customers>

有关如何实现 XML 结果的任何提示或示例。

编辑:

我尝试过下面的java代码:

final String TXT_PATH = "D:/TXT_one.txt";
final String XSLT_PATH = "D:/XSLT_one.xslt";
final String XML_PATH = "D:/test_xml_result_one.xml";

TransformerFactory tFactory = new net.sf.saxon.TransformerFactoryImpl();
Transformer transformer = tFactory.newTransformer(new StreamSource(new File(XSLT_PATH)));
transformer.transform(new StreamSource(new File(TXT_PATH)), new StreamResult(new File(XML_PATH)));

System.out.println("Output written to text file");

和 XSLT 文件:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
xmlns:my="my:my" exclude-result-prefixes="ext my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
<xsl:variable name="lines" as="element()*">
<xsl:for-each select="tokenize(unparsed-text($input), '\r?\n')">
<line><xsl:value-of select="."/></line>
</xsl:for-each>
</xsl:variable>

<results>
<xsl:apply-templates select="$lines/*"/>
</results>
</xsl:template>

<xsl:template match="text()" name="group">
<xsl:param name="lines" select="."/>
<xsl:for-each-group select="$lines[normalize-space()]"
group-starting-with="*[.=('FIRSTNAME', 'LASTNAME')]">
<xsl:element name="{lower-case(.)}s">
<xsl:for-each select="remove(current-group(), 1)">
<xsl:element name="{lower-case(current-group()[1])}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

编译错误如下:

Error at xsl:for-each on line 9 column 68 of XSLT_one.xslt:
XPST0008: Variable input has not been declared (or its declaration is not in scope)

有什么方法可以将输入作为 txt 文件从 java 类传递吗?

最佳答案

首先将线变成节点:

<xsl:variable name="lines" as="element()*">
<xsl:for-each select="tokenize(unparsed-text($input), '\r?\n')">
<line><xsl:value-of select="."/></line>
</xsl:for-each>
</xsl:variable>

然后将它们分组:

<xsl:for-each-group select="$lines[normalize-space()]" 
group-starting-with="*[.=('FIRSTNAME', 'LASTNAME')]">
<xsl:element name="{lower-case(.)}s">
<xsl:for-each select="remove(current-group(), 1)">
<xsl:element name="{lower-case(current-group()[1])}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>

为了完整起见,XSLT 3.0 将其减少为:

<xsl:for-each-group 
select="unparsed-text-lines($input)[normalize-space()]"
group-starting-with=".[.=('FIRSTNAME', 'LASTNAME')]">
<xsl:element name="{lower-case(.)}s">
<xsl:for-each select="tail(current-group())">
<xsl:element name="{lower-case(current-group()[1])}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>

关于java - 使用 xslt 将文本转换为 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33778605/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com