XSL 条件格式

发布于 2024-10-30 17:15:41 字数 1798 浏览 1 评论 0原文

我正在使用 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>

I am using xsl to transform xml to kml format. I would like to add conditional logic to the xsl to switch the styleUrl based on part of an attribute value. The attribute name is FROM_SYSTEM_ID. The format of the attribute value is "A-123-CAM-1" where "CAM" is part of the string to determine which style definition to use (in this case CAM stands for Camera, CAB stands for Cabinet, etc).

How can I parse this attribute to perform the needed style definition switch?

Following is my xsl template:

<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>

Following is a sample of the 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>

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

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

发布评论

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

评论(1

愛上了 2024-11-06 17:15:41

您可以结合使用 substring-after来提取 FROM_SYSTEM_ID 属性的 CAMCAB 部分>substring-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>

You can extract the CAM or CAB portion of the FROM_SYSTEM_ID attribute using a combination of substring-after and substring-before:

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

Putting this together with your stylesheet:

<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>

Applied to this input:

<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>

Produces the following result:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文