【问题标题】:xml xslt transformation: unable to get one node inside anotherxml xslt 转换:无法在另一个节点中获取一个节点
【发布时间】: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 - 我不知道如何为此制定规则。

标签: java xml xslt


【解决方案1】:

FWIW,经过大量努力,以下样式表将生成您问题中显示的结果。我不清楚这里实现的逻辑是否适合所有可能的输入。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" oversion="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="in4" match="inputFour" use="generate-id(preceding-sibling::inputThree[1])" />
<xsl:key name="in5" match="inputFive" use="generate-id(following-sibling::inputFour[1])" />

<xsl:template match="/input">
    <xsl:copy>
        <xsl:apply-templates select="inputOne"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="inputOne">
    <xsl:copy>
        <xsl:copy-of select="numberOne"/>
        <xsl:apply-templates select="../inputTwo"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="inputTwo">
    <xsl:copy>
        <xsl:copy-of select="numberTwo"/>
        <xsl:apply-templates select="../inputThree"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="inputThree">
    <xsl:copy>
        <xsl:copy-of select="numberThree"/>
        <xsl:apply-templates select="key('in4', generate-id())"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="inputFour">
    <xsl:copy>
        <xsl:copy-of select="numberFour"/>
        <xsl:apply-templates select="key('in5', generate-id())"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="inputFive">
    <xsl:copy>
        <xsl:copy-of select="numberFive"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

【讨论】:

  • 它适用于我放在这里的输入文件。需要为真正的输入文件更新它。这是一个很大的帮助。非常感谢迈克尔。欣赏它。
  • 嗨迈克尔,非常感谢您的帮助。它适用于我的输入文件。你我的朋友是救生员。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多