【发布时间】:2020-12-15 14:12:07
【问题描述】:
我是 XSLT 的新手,无法理解它。需要将下面的输入文件转换为下面的输出文件,有人可以帮助我使用 xslt 代码。提前致谢。 输入文件:
<input>
<inputOne>
<numberOne>1</numberOne>
</inputOne>
<inputTwo>
<numberTwo>2</numberTwo>
</inputTwo>
<inputThree>
<numberThree>3</numberThree>
</inputThree>
<inputFive>
<numberFive>5</numberFive>
</inputFive>
<inputFour>
<numberFour>4</numberFour>
</inputFour>
<inputThree>
<numberThree>32</numberThree>
</inputThree>
<inputFive>
<numberFive>52</numberFive>
</inputFive>
<inputFour>
<numberFour>42</numberFour>
</inputFour>
</input>
输出文件:
<input>
<inputOne>
<numberOne>1</numberOne>
<inputTwo>
<numberTwo>2</numberTwo>
<inputThree>
<numberThree>3</numberThree>
<inputFour>
<numberFour>4</numberFour>
<inputFive>
<numberFive>5</numberFive>
</inputFive>
</inputFour>
</inputThree>
<inputThree>
<numberThree>32</numberThree>
<inputFour>
<numberFour>42</numberFour>
<inputFive>
<numberFive>52</numberFive>
</inputFive>
</inputFour>
</inputThree>
</inputTwo>
</inputOne>
</input>
我正在使用 Transformer 对象将 streamsource 转换为 streamresult 对象。转换对象是使用 xsl 文件创建的。
我的 xsl 文件不工作。
xsl代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8"/>
<xsl:template match="/input">
<xsl:copy>
<xsl:apply-templates select="inputOne"/>
</xsl:copy>
</xsl:template>
<xsl:template match="inputOne">
<xsl:apply-templates select="inputTwo"/>
</xsl:template>
<xsl:template match="inputOne[not(inputTwo)]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="inputTwo">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
使用上面的 xsl 我正在尝试获取 inputOne 标记并在其中输入 inputTwo 标记。 但只是得到以下内容:
<input>
<inputOne>
<numberOne>1</numberOne>
</inputOne>
</input>
感谢任何帮助修复 xsl 代码。谢谢。
【问题讨论】:
-
输入 XML 的结构非常可疑。
-
同意。但这就是任务。
-
请说明这里需要应用的逻辑。
-
问题中的逻辑可能并不明显,因为我必须更改标签和元素名称。因为我不能按原样发布它们。但要求应该是显而易见的。 inputOne 是 inputTwo 的父级,它们不重复。文件级标签。 inputThree 是 inpuTwo 的子代, inputFour 是 inputThree 的子代, inputFour 是 InputFive 的子代。标签 InputThree 到 inputFive 重复。传入的文件需要转换为输出文件,并在此处定义父子关系。谢谢
-
一点也不明显 which
inputThree是inputFour等的父级。如果每个级别只查看其以下兄弟姐妹,我可以理解在同一级别没有更接近的前兄弟。但是你有inputFive之前inputFour- 我不知道如何为此制定规则。