Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用XSLT从XML标记中的属性读取值_Xml_Xslt - Fatal编程技术网

如何使用XSLT从XML标记中的属性读取值

如何使用XSLT从XML标记中的属性读取值,xml,xslt,Xml,Xslt,我想使用xslt从下面提供的xml片段中的属性“name”中读取值“enquiry”。有人能帮我吗?我对xslt一无所知 提前感谢。:) 这实际上不是一个属性。这只是一条处理指令的值 我认为获得值的唯一方法是通过一些字符串操作 <xsl:template match="processing-instruction()[name()='table']"> <xsl:value-of select="substring-before(substring-after(.,'n

我想使用xslt从下面提供的xml片段中的属性“name”中读取值“enquiry”。有人能帮我吗?我对xslt一无所知

提前感谢。:)


这实际上不是一个属性。这只是一条处理指令的值

我认为获得值的唯一方法是通过一些字符串操作

<xsl:template match="processing-instruction()[name()='table']">
    <xsl:value-of select="substring-before(substring-after(.,'name=&quot;'),'&quot;')"/>
</xsl:template>

更通用的方法是定义如下模板:

  <xsl:template name="GetPIAttribute">
    <xsl:param name="pi" />
    <xsl:param name="attrName" />

    <xsl:variable name="toFind" select="concat(' ', $attrName, '=')"/>
    <xsl:variable name="piAdjusted" select="concat(' ', normalize-space($pi))"/>

    <xsl:variable name="foundMatch" select="substring-after($piAdjusted, $toFind)" />
    <xsl:if test="$foundMatch">
      <xsl:variable name="delimiter" select="substring($foundMatch, 1, 1)" />
      <xsl:value-of select="substring-before(substring-after($foundMatch, $delimiter), $delimiter)"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="/">
    <xsl:call-template name="GetPIAttribute">
      <xsl:with-param name="pi" select="/processing-instruction()[name() = 'table']" />
      <xsl:with-param name="attrName" select="'name'" />
    </xsl:call-template>
  </xsl:template>

然后您可以调用它来检索您想要的任何伪属性,如下所示:

  <xsl:template name="GetPIAttribute">
    <xsl:param name="pi" />
    <xsl:param name="attrName" />

    <xsl:variable name="toFind" select="concat(' ', $attrName, '=')"/>
    <xsl:variable name="piAdjusted" select="concat(' ', normalize-space($pi))"/>

    <xsl:variable name="foundMatch" select="substring-after($piAdjusted, $toFind)" />
    <xsl:if test="$foundMatch">
      <xsl:variable name="delimiter" select="substring($foundMatch, 1, 1)" />
      <xsl:value-of select="substring-before(substring-after($foundMatch, $delimiter), $delimiter)"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="/">
    <xsl:call-template name="GetPIAttribute">
      <xsl:with-param name="pi" select="/processing-instruction()[name() = 'table']" />
      <xsl:with-param name="attrName" select="'name'" />
    </xsl:call-template>
  </xsl:template>


这种方法的好处是,它将值包含在单引号或双引号中,如果需要提取多个值,您可以重用它。

那么现在……这真的是您的XML示例吗?它实际上不是格式良好的(即没有结束标记),更重要的是,表节点是一条处理指令!你真的想在那里打问号吗?谢谢@TimC——pi很可能是一个错误。在我发布答案之前,我应该注意到这一点。@TimC,实际上,我只获取了代码的一小部分。是的,它有一个结束标记,并且还需要问号。当您需要PI的id“attribute”时,这将不起作用,如
;'“宽”将被返回。因此,foundMatch变量的select属性应该是(concat(“”,normalize space($pi))、concat(“”,$attrName,'=')之后的
子字符串。
。在转换过程中跳过xml文件中的处理指令。是否有任何方法使其可读?