【问题标题】:xslt 1.0 xsl:for each to get required values by comparing nodesxslt 1.0 xsl:for each 通过比较节点来获取所需的值
【发布时间】:2016-03-11 00:32:18
【问题描述】:

由于我是 xslt 新手,如果我的问题标题不正确,请见谅!

我的 xml:

<workorder>
    <wo>wo1234</wo>
    <locspec>
        <attrid>accessrd</attrid>
        <attrvalue>nogothruroad</attrvalue>
        </locspec>
        <locspec>
        <attrid>phone</attrid>
        <attrvalue>99123</attrvalue>
        </locspec>
        <locspec>
        <attrid>accessvehicle</attrid>
        <attrvalue></attrvalue>
    </locspec>
</workorder>

期望的输出:

wo: wo1234
Road to access: nogothrurd
Phone number: 99123
Do you have vehicle access: 

我的 xsl:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:xs="http://www.w3.org/2001/XMLSchema"  version="1.0">

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

    <xsl:template match="workorder">        
        <xsl:apply-templates select="wo" />
        <xsl:apply-templates select="locspec" />
</xsl:template>

    <xsl:template match="wo">
        <xsl:text>wonum: </xsl:text><xsl:value-of select="." />
    </xsl:template>
    <xsl:template match="locspec">
        <xsl:for-each select="locspec/attrid">
        <xsl:text>Road to access: </xsl:text>
        <xsl:value-of select="locspec/attrid"/>
         </xsl:for-each>        
    </xsl:template>
    </xsl:stylesheet>

要求的逻辑是:如果attrid是“accessrd”则获取它的attrvalue,如果attrid是“phone”则获取它的attrvalue,如果attrd是“accessvehicle”则获取它的attrvalue。

坦率地说,我不知道如何将 xsl 编码用于我想要的输出。 请帮我。提前致谢!

当前输出为空。

【问题讨论】:

    标签: xml xslt xpath xslt-1.0


    【解决方案1】:

    试试这个:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
    
      <xsl:template match="wo">
        <xsl:value-of select="concat(name(), ': ', .)"/>
      </xsl:template>
    
      <xsl:template match="locspec">
        <xsl:text>
    </xsl:text>
        <xsl:choose>
          <xsl:when test="attrid = 'accessrd'">
            <xsl:text>Road to access</xsl:text>
          </xsl:when>
          <xsl:when test="attrid = 'phone'">
            <xsl:text>Phone number</xsl:text>
          </xsl:when>
          <xsl:when test="attrid = 'accessvehicle'">
            <xsl:text>Do you have vehicle access</xsl:text>
          </xsl:when>
        </xsl:choose>
        <xsl:value-of select="concat(': ', attrvalue)"/>
      </xsl:template>
    
      <xsl:template match="text()"/>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 感谢您的帮助。您能否帮我进行比较,如果 attrid=accessrd,那么只能得到那个值。因为就我而言, attrid 会有所不同。可能是我的愿望输出具有误导性。我已经编辑过了。
    • 在某些情况下,我只想从 xml 中获取电话号码。请建议
    • 非常感谢您的帮助 :)
    猜你喜欢
    • 2017-01-13
    • 2015-01-09
    • 2016-07-03
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    相关资源
    最近更新 更多