加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – XSL:确定祖先的序列

发布时间:2020-12-16 23:18:22 所属栏目:百科 来源:网络整理
导读:我有一个 XML样本,看起来像: ?xml version="1.0" encoding="UTF-8"?root location id="1" address1600 Pennsylvania Avenue/address address211B Baker Street/address /location location id="1" address17 Cherry Tree Lane/address /location location i
我有一个 XML样本,看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <location id="1">
        <address>1600 Pennsylvania Avenue</address>
        <address>211B Baker Street</address>
    </location>
    <location id="1">
        <address>17 Cherry Tree Lane</address>
    </location>
    <location id="2">
        <address>350 5th Avenue</address>
    </location>
</root>

我想生成如下输出:

<?xml version="1.0" encoding="utf-8"?>
<result>
    <location id="1">
        <address addressId="1">1600 Pennsylvania Avenue</address>
        <address addressId="2">211B Baker Street</address>
    </location>
    <location id="1">
        <address addressId="3">17 Cherry Tree Lane</address>
    </location>
    <location id="2">
        <address addressId="1">350 5th Avenue</address>
    </location>
</result>

这样,addressId反映了具有相同id属性的所有位置实例的地址序列.

我在想<xsl:number>将是我的答案,但我的尝试失败了:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

    <xsl:template match="/root">
        <result>
            <xsl:for-each select="location">
                <location>
                    <xsl:attribute name="id">
                        <xsl:value-of select="@id" />
                    </xsl:attribute>

                    <xsl:for-each select="address">
                        <address>
                            <xsl:attribute name="addressId">
                                <xsl:number count="//location[@id = ../@id]/address" level="any" />
                            </xsl:attribute>

                            <!--                            
                              The rest are just my debugging attempts; 
                                curiously addressId3 and addressId4 return
                                different values?
                              -->

                            <!--
                            <xsl:attribute name="addressId2">
                                <xsl:number count="//location[@id = parent::location/@id]/address" level="any" />
                            </xsl:attribute>

                            <xsl:attribute name="addressId3">
                                <xsl:value-of select="count(//location[@id=../@id]/address)" />
                            </xsl:attribute>

                            <xsl:variable name="locId">
                                <xsl:value-of select="../@id" />
                            </xsl:variable>

                            <xsl:attribute name="addressId4">
                                <xsl:value-of select="count(//location[@id=$locId]/address)" />
                            </xsl:attribute>

                            <xsl:attribute name="addressId5">
                                <xsl:number count="//location[@id = '1']/address" level="any" />
                            </xsl:attribute>
                              -->

                            <xsl:value-of select="." />
                        </address>
                    </xsl:for-each>
                </location>
            </xsl:for-each>
        </result>
    </xsl:template>
</xsl:stylesheet>

解决方法

这是一种解决方法,通过简单地计算具有相同id属性的位置parent的前面的地址元素:

<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="location/address">
  <xsl:copy>
    <xsl:attribute name="addressId">
      <xsl:value-of select="count(preceding::address[../@id = current()/../@id]) + 1"/>
    </xsl:attribute>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读