XSLT 1.0:如何从匹配特定条件的前一节点检索文本子字符串

时间:2013-11-12 20:56:08

标签: xml xslt

我正在尝试实现一个XSLT 1.0处理器使用的样式表,但是无法弄清楚如何从匹配某些条件的前一个节点检索文本子字符串。这些条件在下面的XSL样式表片段中描述,但首先是输入文档的示例:

<?xml version="1.0" encoding="UTF-8"?>
<dataRoot>
   <dataRow>
      <a0>Blank 25/10/2013</a0>
   </dataRow>
   <dataRow>
      <a0>2013006430</a0>
      <a5>31.84</a5>
   </dataRow>
   <dataRow>
      <a0>Blank 24/10/2013</a0>
   </dataRow>
   <dataRow>
      <a0>2013006409</a0>
      <a5>0.5504</a5>
   </dataRow>
   <dataRow>
      <a0>2013006410</a0>
      <a5>7.376</a5>
   </dataRow>
</dataRoot>

...这里是XSL样式表,我需要弄清楚如何获取内联描述的'dateAnalysed'值:

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <INBOUND>
            <xsl:for-each select="dataRoot/dataRow">

                <xsl:variable name="dateAnalysed">
                    <!-- How can I implement the following as an xpath expression? -->
                    <xsl:value-of select="text following 'Blank ' in the last preceding a0 node with text that starts with 'Blank '"/>
                </xsl:variable>

                <xsl:variable name="sampleId">
                    <xsl:value-of select="a0"/>
                </xsl:variable>
                <!-- Do stuff with $dateAnalysed and $sampleId -->
            </xsl:for-each>
        </INBOUND>
    </xsl:template>
</xsl:stylesheet>

为了给出更多的上下文,输出应该是什么样的:

<INBOUND>
  <INBOX_SAMPLE>
    <SAMPLE_ID>2013006430</SAMPLE_ID>
    <PARAMETER_NAME>A5</PARAMETER_NAME>
    <SRESULT>31.84</SRESULT>
  </INBOX_SAMPLE>
  <INBOX_SAMPLE>
    <SAMPLE_ID>2013006430</SAMPLE_ID>
    <PARAMETER_NAME>Date</PARAMETER_NAME>
    <SRESULT>25/10/2013</SRESULT>
  </INBOX_SAMPLE>
  <INBOX_SAMPLE>
    <SAMPLE_ID>2013006409</SAMPLE_ID>
    <PARAMETER_NAME>A5</PARAMETER_NAME>
    <SRESULT>0.5504</SRESULT>
  </INBOX_SAMPLE>
  <INBOX_SAMPLE>
    <SAMPLE_ID>2013006409</SAMPLE_ID>
    <PARAMETER_NAME>Date</PARAMETER_NAME>
    <SRESULT>24/10/2013</SRESULT>
  </INBOX_SAMPLE>
  <INBOX_SAMPLE>
    <SAMPLE_ID>2013006410</SAMPLE_ID>
    <PARAMETER_NAME>A5</PARAMETER_NAME>
    <SRESULT>7.376</SRESULT>
  </INBOX_SAMPLE>
  <INBOX_SAMPLE>
    <SAMPLE_ID>2013006410</SAMPLE_ID>
    <PARAMETER_NAME>Date</PARAMETER_NAME>
    <SRESULT>24/10/2013</SRESULT>
  </INBOX_SAMPLE>
</INBOUND>

1 个答案:

答案 0 :(得分:0)

根据您的描述,我相信您正在寻找这个xpath:

substring-after(preceding-sibling::dataRow[starts-with(a0,'Blank ')]/a0,'Blank ')

要将其细分,preceding-sibling::dataRow会找到之前的dataRow元素,[starts-with(a0,'Blank ')会将其限制为第一个包含a0元素且内容以'Blank '开头的元素。最后,substring-after函数应用于其中a0元素的值,并返回'Blank '之后的剩余字符串。