从XML(XSLT)获取第二个值

时间:2013-02-21 07:52:28

标签: xml xslt transform

我有以下XML,我只想要第二个值... 我怎么能这样做?

XML:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
        <GetVersionCollectionResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
          <GetVersionCollectionResult>
            <Versions>
              <Version AssignedTo="value" />
              <Version AssignedTo="This value I want to get" />
              <Version AssignedTo="value" />
              <Version AssignedTo="value" />
            </Versions>
          </GetVersionCollectionResult>
        </GetVersionCollectionResponse>
      </soap:Body>
    </soap:Envelope>

获取每个值的XML请求 (如何将其更改为仅获取第二个值?)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" indent="no"></xsl:output>
        <xsl:template name="ShowVariables" match="/" >
            <xsl:for-each select="//*[name()='Version']">
                <xsl:value-of select="@AssignedTo" />
            </xsl:for-each>
        </xsl:template>
</xsl:stylesheet>

提前感谢您的帮助。

此致 西蒙

1 个答案:

答案 0 :(得分:0)

您还必须declare样式表中的XML文档中使用的namespaces

样式表

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:sharepoint="http://schemas.microsoft.com/sharepoint/soap/">

  <xsl:output method="text" indent="no"/>

  <xsl:template match="/">
    <xsl:value-of select="//sharepoint:Version[2]/@AssignedTo"/>
  </xsl:template>
</xsl:stylesheet>

或者,如果你想更精确:

<xsl:value-of select="soap:Envelope/soap:Body/sharepoint:GetVersionCollectionResponse/sharepoint:GetVersionCollectionResult/sharepoint:Versions/sharepoint:Version[2]/@AssignedTo"/>

输出

This value I want to get