XSLT 用于 (i) 检查标记是否存在且不为空 (ii) 如果重复子节点'value'之一在某个位置包含字符串,则提取父节点



我在实现以下功能时遇到问题:

(i) 我需要检查xml中的标记。如果它存在并且不为空,我应该得到它的值,否则就是默认值。我写xslt如下:

<xsl:if test="relation">
  <xsl:choose>
    <xsl:when test="boolean(relation/termId) and string(relation/termId) != ''">
      <xsl:value-of select="relation/termId" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>DefaultTermId</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:if>

因此,relationship/termId是存在的,并且有一些值(比如<termId>x</termId>,我应该得到x)xpath和所有这些都很好。当termId标记不存在时(=>我得到的是"DefaultTermId"),它工作正常,但当标记没有值时返回一个空格。我的意思是,当is是<termId></termId>时,我得到的是一个空格,而不是"DefaultTermId"。我也试过relation/termId/text() != '',但没有用。

(ii)另一个问题是=>我的xml如下所示:

<GetSavedReportResponse xmlns="">
  <ResponseType>Success</ResponseType>
  <FileModifiedDateTime>2012-01-03T17:05:04</FileModifiedDateTime>
  <FileSizeBytes>7816</FileSizeBytes>
  <FileDataFormat>XML</FileDataFormat>
  <FileData>
    <Zthes>
      <term>
        <termId>49555</termId>
        <termUpdate>add</termUpdate>
        <termName>Active Personnel</termName>
        <termVocabulary>People Status Global</termVocabulary>
        <termVocabulary>Employee Status Global</termVocabulary>
        <termCategory>PDA</termCategory>
        <termCategory>PDI</termCategory>
        <termCategory>GLB</termCategory>
        <relation weight="100">
          <termId>49556</termId>
          <relationType>EQ</relationType>
          <termName>term name</termName>
          <termVocabulary>term vocabulary</termVocabulary>
        </relation>
        <relation weight="100">
          <termId>49556</termId>
          <relationType>BT</relationType>
          <termName>General Active Personnel</termName>
          <termVocabulary>People Status Global Updated</termVocabulary>
        </relation>
      </term>
      <term>
        <termId>49554</termId>
        <termUpdate>add</termUpdate>
        <termName>General Active Personnel</termName>
        <termVocabulary>People Status Global</termVocabulary>
        <termCategory>PDI</termCategory>
      </term>
    </Zthes>
  </FileData>
</GetSavedReportResponse>   

在这里,一个术语可以有多个termCategory标签。我需要检查这些节点中是否有任何一个包含指定的子字符串,如果是,我需要提取整个术语节点。我试过如下:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <xsl:if test="termCategory">
      <xsl:if test="substring(FileData/Zthes/term/termCategory, 1, 2) = 'GL'">
        <xsl:copy>
          <xsl:apply-templates select="term"/>
        </xsl:copy>
      </xsl:if>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

但是,它运行不好。这里的输出应该是:

<GetSavedReportResponse xmlns="">
  <ResponseType>Success</ResponseType>
  <FileModifiedDateTime>2012-01-03T17:05:04</FileModifiedDateTime>
  <FileSizeBytes>7816</FileSizeBytes>
  <FileDataFormat>XML</FileDataFormat>
  <FileData>
    <Zthes>
      <term>
        <termId>49555</termId>
        <termUpdate>add</termUpdate>
        <termName>Active Personnel</termName>
        <termVocabulary>People Status Global</termVocabulary>
        <termVocabulary>Employee Status Global</termVocabulary>
        <termCategory>PDA</termCategory>
        <termCategory>PDI</termCategory>
        <termCategory>GLB</termCategory>
        <relation weight="100">
          <relationType>EQ</relationType>
          <termName>term name</termName>
          <termVocabulary>term vocabulary</termVocabulary>
        </relation>
        <relation weight="100">
          <relationType>BT</relationType>
          <termName>General Active Personnel</termName>
          <termVocabulary>People Status Global Updated</termVocabulary>
        </relation>
      </term>
      <term>
    </Zthes>
  </FileData>
</GetSavedReportResponse>

p.s:子字符串的位置没有固定,所以我不能使用startswitch等,。我必须使用SubString。因此,在上面的例子中:第一个术语的第三个术语类别包含"GL",因此应该检索它。第二个术语有一个术语Category,但不包含"GL",因此不应检索它。请帮我哪里做错了。提前谢谢。

问候

您希望从输出中排除任何没有termCategoryterm,其字符串值以"GL":开头

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
 <xsl:template match="term[not(termCategory[starts-with(.,'GL')])]"/>
</xsl:stylesheet>

当此转换应用于所提供的XML文档时

<GetSavedReportResponse xmlns="">
    <ResponseType>Success</ResponseType>
    <FileModifiedDateTime>2012-01-03T17:05:04</FileModifiedDateTime>
    <FileSizeBytes>7816</FileSizeBytes>
    <FileDataFormat>XML</FileDataFormat>
    <FileData>
        <Zthes>
            <term>
                <termId>49555</termId>
                <termUpdate>add</termUpdate>
                <termName>Active Personnel</termName>
                <termVocabulary>People Status Global</termVocabulary>
                <termVocabulary>Employee Status Global</termVocabulary>
                <termCategory>PDA</termCategory>
                <termCategory>PDI</termCategory>
                <termCategory>GLB</termCategory>
                <relation weight="100">
                    <relationType>EQ</relationType>
                    <termName>term name</termName>
                    <termVocabulary>term vocabulary</termVocabulary>
                </relation>
                <relation weight="100">
                    <relationType>BT</relationType>
                    <termName>General Active Personnel</termName>
                    <termVocabulary>People Status Global Updated</termVocabulary>
                </relation>
            </term>
            <term>
                <termId>49554</termId>
                <termUpdate>add</termUpdate>
                <termName>General Active Personnel</termName>
                <termVocabulary>People Status Global</termVocabulary>
                <termCategory>PDI</termCategory>
            </term>
        </Zthes>
    </FileData>
</GetSavedReportResponse>

生成所需的正确结果

<GetSavedReportResponse>
   <ResponseType>Success</ResponseType>
   <FileModifiedDateTime>2012-01-03T17:05:04</FileModifiedDateTime>
   <FileSizeBytes>7816</FileSizeBytes>
   <FileDataFormat>XML</FileDataFormat>
   <FileData>
      <Zthes>
         <term>
            <termId>49555</termId>
            <termUpdate>add</termUpdate>
            <termName>Active Personnel</termName>
            <termVocabulary>People Status Global</termVocabulary>
            <termVocabulary>Employee Status Global</termVocabulary>
            <termCategory>PDA</termCategory>
            <termCategory>PDI</termCategory>
            <termCategory>GLB</termCategory>
            <relation weight="100">
               <relationType>EQ</relationType>
               <termName>term name</termName>
               <termVocabulary>term vocabulary</termVocabulary>
            </relation>
            <relation weight="100">
               <relationType>BT</relationType>
               <termName>General Active Personnel</termName>
               <termVocabulary>People Status Global Updated</termVocabulary>
            </relation>
         </term>
      </Zthes>
   </FileData>
</GetSavedReportResponse>

解释:用一个模板覆盖标识规则,该模板匹配任何没有值以字符串"GL"开头的termCategory子级的term。这个重写模板有一个空的主体——因此有效地从输出中排除(删除)任何匹配的节点。

不确定,但可能是你想要吗

      <xsl:value-of select="relation/termId/text()" />

相反?

尽管您的xml中没有任何relation/termId。。。

此外,在第二个示例中,相对于GetSavedReportResponse节点访问FileData,而不是在上下文中评估它的/

最新更新