xml - xslt - 将所选元素转换为属性

xml - xslt - transform a chosen element into attribute

我正在尝试将一个 XML 文档转换为一个新文档,其中唯一的一个元素将自身转换为一个属性,并以相同的方式保留文档树的其余部分...这是XML 文档

    <?xml version="1.0" encoding="UTF-8"?>
    <cities>
    <city>
        <cityID>c1</cityID>
        <cityName>Atlanta</cityName>
        <cityCountry>USA</cityCountry>
        <cityPop>4000000</cityPop>
        <cityHostYr>1996</cityHostYr>   

    </city>

    <city>
        <cityID>c2</cityID>
        <cityName>Sydney</cityName>
        <cityCountry>Australia</cityCountry>
        <cityPop>4000000</cityPop>
        <cityHostYr>2000</cityHostYr>   
        <cityPreviousHost>c1</cityPreviousHost >   
    </city>

    <city>
        <cityID>c3</cityID>
        <cityName>Athens</cityName>
        <cityCountry>Greece</cityCountry>
        <cityPop>3500000</cityPop>
        <cityHostYr>2004</cityHostYr>   
        <cityPreviousHost>c2</cityPreviousHost >   
    </city>

    </cities>

我正在尝试将 "cityID" 元素放入 "city" 的属性中并保留其余部分。到目前为止,这是我的 .xsl。不幸的是,它似乎丢失了其余的元素:

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

    <xsl:template match="/">
        <cities>
            <xsl:apply-templates/>
        </cities>
    </xsl:template>


    <xsl:template match="city">
        <xsl:element name="city" use-attribute-sets="NameAttributes"/>
    </xsl:template>


    <xsl:attribute-set name="NameAttributes">
        <xsl:attribute name="cityID">
            <xsl:value-of select="cityID"/>
        </xsl:attribute>
    </xsl:attribute-set>

    <xsl:template match="/ | @* | node()">
         <xsl:copy>
             <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>


</xsl:stylesheet>

我会用

<xsl:template match="city">
    <city cityID="{cityID}">
       <xsl:apply-templates select="node() except cityID"/> <!-- except is XSLT 2.0, use select="node()[not(self::cityID)]" for XSLT 1.0 -->
    </city>
</xsl:template>

或为

编写模板
<xsl:template match="cityID">
  <xsl:attribute name="{name()}" select="."/>
  <!-- XSLT 1.0 <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute> -->
</xsl:template>

所有属性都需要在子节点之前创建,因此使用 <xsl:strip-space elements="*"/> 可能是该方法工作所必需的。

这两个建议都假定存在您拥有的身份转换模板。

匹配城市元素的模板不处理该元素的子元素。将其替换为以下内容:

<xsl:template match="city">
    <xsl:element name="city" use-attribute-sets="NameAttributes">
      <xsl:apply-templates select="node()[name()!='cityID']" />
    </xsl:element>
</xsl:template>

就这么简单(完整的 XSLT 1.0)转换

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="city">
    <city cityID="{cityID}">
      <xsl:apply-templates select="@*|node()"/>
    </city>
  </xsl:template>
  <xsl:template match="cityID"/>
</xsl:stylesheet>