2

我有以下xml:

<Root>    
        <Item>
          <CityCode>ALV</CityCode>
          <AirportCode>ALV</AirportCode>
          <CityName>ANDORRA LA VELLA</CityName>
          <AirportName>Andorra La Vella Hlpt</AirportName>
          <StateCode></StateCode>
          <CountryCode>AD</CountryCode>
          <AirportTypeCode>8</AirportTypeCode>
          <AirportTypeName>Heliport, not scheduled</AirportTypeName>    
       </Item>       
</Root>

我需要一个 xslt 来得到这个:

<Root>
   <Item>
     <CityCode>ALV</CityCode>
     <CityName>ANDORRA LA VELLA</CityName>
     <CityNameComplete> ANDORRA LA VELLA (ALV) - Andorra La Vella Hlpt </CityNameComplete>
   <Item>
<Root>

我知道如何获得前 2 个节点,我不知道如何“插入”最后一个。

4

3 回答 3

1

有多种方法可以将它嵌入到您的完整 XSLT 中,但假设您将元素视为原始 Xml 文档<CityNameComplete>中元素的替换。<AirportName>

然后,关键是 XSLT 的<value-of>元素:

<xsl:template match="/Root/Item/AirportName">
    <CityNameComplete><xsl:value-of select="../CityName"/> (<xsl:value-of select="../AirportCode"/>) - <xsl:value-of select="."/></CityNameComplete>
</xsl:template>

这将产生一个

<CityNameComplete>ANDORRA LA VELLA (ALV) - Andorra La Vella Hlpt</CityNameComplete>

结果中的元素。

更新:如果您真的想要完整城市名称周围的空格,请将其添加<xsl:text> </xsl:text>到样式表中:

<xsl:template match="/Root/Item/AirportName">
    <CityNameComplete><xsl:text> </xsl:text><xsl:value-of select="../CityName"/> (<xsl:value-of select="../AirportCode"/>) - <xsl:value-of select="."/><xsl:text> </xsl:text></CityNameComplete>
</xsl:template>

结果:

<CityNameComplete> ANDORRA LA VELLA (ALV) - Andorra La Vella Hlpt </CityNameComplete>

更新 2:包括 IATA 代码。

更新 3:插入节点的另一种解决方案是将其添加到<Item>元素的模板中:

<xsl:template match="/Root/Item">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
        <CityNameComplete><xsl:text> </xsl:text><xsl:value-of select="CityName"/> (<xsl:value-of select="AirportCode"/>) - <xsl:value-of select="AirportName"/><xsl:text> </xsl:text></CityNameComplete>
    </xsl:copy>
</xsl:template>

请注意,您必须删除您不想完全获得输出的节点 - 因为您没有要求这样做,而且我不想弄乱答案,所以我没有包含任何关于如何这样做。

于 2012-06-27T12:14:41.470 回答
1

一种方法是在标识模板的基础上构建,并有一个额外的模板来匹配item元素,然后使用concat函数在其中输出CityNameComplete元素:

<xsl:template match="Item">
  <Item>
     <xsl:apply-templates select="@*|node()"/>
     <CityNameComplete>
        <xsl:value-of select="concat(CityName, ' (', AirportCode, ') ', AirportName)"/>
     </CityNameComplete>
  </Item>
</xsl:template>

您还需要一个模板来忽略您不想输出的所有元素。在这种情况下,除CityCodeCityName元素之外的所有内容

<xsl:template match="Item/*[not(self::CityCode|self::CityName)]"/>

这是完整的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="Item/*[not(self::CityCode|self::CityName)]"/>

   <xsl:template match="Item">
      <Item>
         <xsl:apply-templates select="@*|node()"/>
         <CityNameComplete>
            <xsl:value-of select="concat(CityName, ' (', AirportCode, ') ', AirportName)"/>
         </CityNameComplete>
      </Item>
   </xsl:template>

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

当应用于您的输入 XML 时,将输出以下内容

<Root>
   <Item>
     <CityCode>ALV</CityCode>
     <CityName>ANDORRA LA VELLA</CityName>
     <CityNameComplete>ANDORRA LA VELLA (ALV) Andorra La Vella Hlpt</CityNameComplete>
   </Item>
</Root>
于 2012-06-27T12:18:22.953 回答
0

这是一个可以处理缺失<AirportCode>和/或的版本<AirportName>

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

    <!--Identity template. Matches everything that isn't matched
        by another template and copies it without modification.-->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Item">
        <!--Copy the current element (Item) to the output.-->
        <xsl:copy>
            <!--Apply templates to attributes of Item and also the
            CityCode and CityName elements. All other nodes of
            Item will not be processed.-->
            <xsl:apply-templates select="CityCode|CityName|@*"/>
            <CityNameComplete>
                <!--Output the value of CityName.-->
                <xsl:value-of select="CityName"/>
                <!--Apply the template for AirportCode. If it doesn't exist,
                nothing will be output.-->
                <xsl:apply-templates select="AirportCode"/>
                <!--Apply the template for AirportName. If it doesn't exist,
                nothing will be output.-->
                <xsl:apply-templates select="AirportName"/>
            </CityNameComplete>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="AirportCode">
        <!--Output the value of AirportCode, surrounded by parenthesis and
        preceded by a space.-->
        <xsl:value-of select="concat(' (',.,')')"/>
    </xsl:template>

    <xsl:template match="AirportName">
        <!--Output the value of AirportName, preceded by a "space dash space".-->
        <xsl:value-of select="concat(' - ',.)"/>
    </xsl:template>

</xsl:stylesheet>
于 2012-06-27T19:15:25.250 回答