XSL条件格式

时间:2011-04-05 17:03:48

标签: xslt

我正在使用xsl将xml转换为kml格式。我想向xsl添加条件逻辑,以根据属性值的一部分切换styleUrl。属性名称为FROM_SYSTEM_ID。属性值的格式为“A-123-CAM-1”,其中“CAM”是字符串的一部分,用于确定要使用的样式定义(在本例中,CAM代表Camera,CAB代表Cabinet等)。

如何解析此属性以执行所需的样式定义开关?

以下是我的xsl模板:

<xsl:template match="Line">
    <Folder>
      <name>
        Lines
        <!--<xsl:value-of select="@name"/>-->
      </name>
      <xsl:for-each select="Row">
        <Placemark>
          <name>
            <xsl:value-of select="@FROM_SYSTEM_ID"/>
          </name>
          <description>
            <xsl:value-of select="@TO_SYSTEM_ID"/>
          </description>
          <styleUrl>#msn_open-diamond00</styleUrl>
          <LineString>
            <tessellate>1</tessellate>
            <coordinates>
              <xsl:value-of select="@FromLong"/>,<xsl:value-of select="@FromLat"/>,0 <xsl:value-of select="@ToLong"/>,<xsl:value-of select="@ToLat"/>,0
            </coordinates>
          </LineString>
        </Placemark>
      </xsl:for-each>
    </Folder>
  </xsl:template>

以下是XML的示例:

<Line>
    <Row PrimaryRoute="A-123" FROM_SYSTEM_ID="A-123-CAB-1"
        TO_SYSTEM_ID="A-123-CAM-3" FromLat="42.624948852000" 
        FromLong="-83.107221652500"
        ToLat="42.624940325900" ToLong="-83.107353167000" />
    <Row PrimaryRoute="A-123" FROM_SYSTEM_ID="A-123-CAM-1"
        TO_SYSTEM_ID="A-123-HH-16" FromLat="42.641662528600" 
        FromLong="-83.151500129600"
        ToLat="42.641709802200" ToLong="-83.151552587600" />
    <!-- additional rows here -->
</Line>

1 个答案:

答案 0 :(得分:0)

您可以使用CAMCAB的组合提取FROM_SYSTEM_ID属性的substring-aftersubstring-before部分:

<xsl:value-of select="
    substring-before(
        substring-after(
            substring-after(@FROM_SYSTEM_ID, '-'), '-'), '-')"/>

将它与样式表放在一起:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Line">
    <Folder>
      <name>
        Lines
        <!--<xsl:value-of select="@name"/>-->
      </name>
      <xsl:for-each select="Row">
        <Placemark>
          <name>
            <xsl:value-of select="@FROM_SYSTEM_ID"/>
          </name>
          <description>
            <xsl:value-of select="@TO_SYSTEM_ID"/>
          </description>
          <styleUrl>
              <xsl:value-of select="
                  substring-before(
                      substring-after(
                          substring-after(@FROM_SYSTEM_ID, '-'), '-'), '-')"/>
          </styleUrl>
          <LineString>
            <tessellate>1</tessellate>
            <coordinates>
              <xsl:value-of select="@FromLong"/>,<xsl:value-of select="@FromLat"/>,0 <xsl:value-of select="@ToLong"/>,<xsl:value-of select="@ToLat"/>,0
            </coordinates>
          </LineString>
        </Placemark>
      </xsl:for-each>
    </Folder>
  </xsl:template>
</xsl:stylesheet>

应用于此输入:

<Line>
    <Row PrimaryRoute="A-123" FROM_SYSTEM_ID="A-123-CAB-1"
        TO_SYSTEM_ID="A-123-CAM-3" FromLat="42.624948852000" 
        FromLong="-83.107221652500"
        ToLat="42.624940325900" ToLong="-83.107353167000" />
    <Row PrimaryRoute="A-123" FROM_SYSTEM_ID="A-123-CAM-1"
        TO_SYSTEM_ID="A-123-HH-16" FromLat="42.641662528600" 
        FromLong="-83.151500129600"
        ToLat="42.641709802200" ToLong="-83.151552587600" />
</Line>

产生以下结果:

<Folder>
    <name>Lines</name>
    <Placemark>
        <name>A-123-CAB-1</name>
        <description>A-123-CAM-3</description>
        <styleUrl>CAB</styleUrl>
        <LineString>
            <tessellate>1</tessellate>
            <coordinates>-83.107221652500,42.624948852000,0
                -83.107353167000,42.624940325900,0
            </coordinates>
        </LineString>
    </Placemark>
    <Placemark>
        <name>A-123-CAM-1</name>
        <description>A-123-HH-16</description>
        <styleUrl>CAM</styleUrl>
        <LineString>
            <tessellate>1</tessellate>
            <coordinates>-83.151500129600,42.641662528600,0
                -83.151552587600,42.641709802200,0
            </coordinates>
        </LineString>
    </Placemark>
</Folder>