XML 中的前同级

发布于 2024-10-30 09:31:04 字数 1677 浏览 5 评论 0原文

我有这样的 XML 数据:

<items>
<data>2</data>  
<listElement>       
    <amounts>           
        <period_id>1</period_id>            
        <amount>5</amount>
    </amounts>
    <amounts>
        <period_id>2</period_id>            
        <amount>6</amount>
    </amounts>
    <amounts>           
        <period_id>3</period_id>            
        <amount>7</amount>
    </amounts>
    <amounts>           
        <period_id>8</period_id>            
        <amount>89</amount>
    </amounts>
</listElement>
</items>
<items>
<data></data>   
<listElement>       
    <amounts>           
        <period_id>4</period_id>            
        <amount>55</amount>
    </amounts>
    <amounts>
        <period_id>5</period_id>            
        <amount>9</amount>
    </amounts>
    <amounts>           
        <period_id>6</period_id>            
        <amount>20</amount>
    </amounts>
    <amounts>           
        <period_id>7</period_id>            
        <amount>80</amount>
    </amounts>
</listElement>
</items>

在我的 xsl 代码中,我位于节点金额内,我想获取标签“数据”的值,谁是该标签金额的父级?

我正在使用 xalan 与 xslt1.0 和 apache fop

注意:我尝试过:

<xsl:value-of select="preceding-sibling::*data[normalize-space(.)]">
</xsl:value-of> 

但仍然错误。

I have a XML data like that:

<items>
<data>2</data>  
<listElement>       
    <amounts>           
        <period_id>1</period_id>            
        <amount>5</amount>
    </amounts>
    <amounts>
        <period_id>2</period_id>            
        <amount>6</amount>
    </amounts>
    <amounts>           
        <period_id>3</period_id>            
        <amount>7</amount>
    </amounts>
    <amounts>           
        <period_id>8</period_id>            
        <amount>89</amount>
    </amounts>
</listElement>
</items>
<items>
<data></data>   
<listElement>       
    <amounts>           
        <period_id>4</period_id>            
        <amount>55</amount>
    </amounts>
    <amounts>
        <period_id>5</period_id>            
        <amount>9</amount>
    </amounts>
    <amounts>           
        <period_id>6</period_id>            
        <amount>20</amount>
    </amounts>
    <amounts>           
        <period_id>7</period_id>            
        <amount>80</amount>
    </amounts>
</listElement>
</items>

In my xsl code I'm inside a node amounts and I want to get the value of the tag "data" who is parent of this tag amounts?

I'm using xalan with xslt1.0 and apache fop

Note: I tried with:

<xsl:value-of select="preceding-sibling::*data[normalize-space(.)]">
</xsl:value-of> 

But still wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流绪微梦 2024-11-06 09:31:04

在我的 xsl 代码中,我位于一个节点内
金额,我想得到的值
标签“data”是该标签的父标签
标签金额

请注意,示例中的 data 元素是 itemsamount 祖父)的子元素。

使用这个:

../../data

也可以这样:

preceding::data[1]

但是必须保证每个items中都有一个data

如果您确实想使用 preceding-sibling 轴,那么:

../preceding-sibling::data

in my xsl code i'm inside a node
amounts and i want to get the value of
the tag "data" who is parent of this
tag amounts

Do note that data element is a child of items (amount grandparent) in your sample.

Use this:

../../data

Also this:

preceding::data[1]

But it must be guaranteed that there is going to be one data in every items.

If you really want to use preceding-sibling axis then:

../preceding-sibling::data
压抑⊿情绪 2024-11-06 09:31:04

amounts 元素的上下文:

../preceding-sibling::data[1]

以下样式表将前面的 data 元素复制到每个 amounts 元素中,使文档的其余部分保持不变:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="amounts">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
            <xsl:copy-of select="../preceding-sibling::data[1]" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

当应用于源文档时,它会产生以下输出:

<root>
    <items>
        <data>2</data>
        <listElement>
            <amounts>
                <period_id>1</period_id>
                <amount>5</amount>
                <data>2</data>
            </amounts>
            <amounts>
                <period_id>2</period_id>
                <amount>6</amount>
                <data>2</data>
            </amounts>
            <amounts>
                <period_id>3</period_id>
                <amount>7</amount>
                <data>2</data>
            </amounts>
            <amounts>
                <period_id>8</period_id>
                <amount>89</amount>
                <data>2</data>
            </amounts>
        </listElement>
    </items>
    <items>
        <data />
        <listElement>
            <amounts>
                <period_id>4</period_id>
                <amount>55</amount>
                <data />
            </amounts>
            <amounts>
                <period_id>5</period_id>
                <amount>9</amount>
                <data />
            </amounts>
            <amounts>
                <period_id>6</period_id>
                <amount>20</amount>
                <data />
            </amounts>
            <amounts>
                <period_id>7</period_id>
                <amount>80</amount>
                <data />
            </amounts>
        </listElement>
    </items>
</root>

From the context of an amounts element:

../preceding-sibling::data[1]

The following stylesheet copies the preceding data element into each amounts element, leaving the rest of the document unchanged:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="amounts">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
            <xsl:copy-of select="../preceding-sibling::data[1]" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

It produces the following output when applied to your source document:

<root>
    <items>
        <data>2</data>
        <listElement>
            <amounts>
                <period_id>1</period_id>
                <amount>5</amount>
                <data>2</data>
            </amounts>
            <amounts>
                <period_id>2</period_id>
                <amount>6</amount>
                <data>2</data>
            </amounts>
            <amounts>
                <period_id>3</period_id>
                <amount>7</amount>
                <data>2</data>
            </amounts>
            <amounts>
                <period_id>8</period_id>
                <amount>89</amount>
                <data>2</data>
            </amounts>
        </listElement>
    </items>
    <items>
        <data />
        <listElement>
            <amounts>
                <period_id>4</period_id>
                <amount>55</amount>
                <data />
            </amounts>
            <amounts>
                <period_id>5</period_id>
                <amount>9</amount>
                <data />
            </amounts>
            <amounts>
                <period_id>6</period_id>
                <amount>20</amount>
                <data />
            </amounts>
            <amounts>
                <period_id>7</period_id>
                <amount>80</amount>
                <data />
            </amounts>
        </listElement>
    </items>
</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文