从XSLT中的函数返回元素的值类型

问题描述 投票:0回答:1

这是我的XML

<report>
    <format-inputs>
        <narrative-entity-ids>
            <entity id="28495795" type-cdf-meaning="DIAGNOSES"/>
            <entity id="28495741" type-cdf-meaning="DIAGNOSES"/>
            <entity id="28495471" type-cdf-meaning="DIAGNOSES"/>
        </narrative-entity-ids>
    </format-inputs>
</report> 

我在commonFunction.xslt中创建了一个函数

<xsl:function name="cdocfx:createEntityIdList" >
        <xsl:param name="formatInputsNodes"/>

        <xsl:if test="fn:exists(n:report/n:format-inputs)"
        <xsl:variable name="entityIdList" as="element()*">
            <xsl:for-each select="$formatInputsNodes/n:narrative-entity-ids/n:entity">
                <Item><xsl:value-of select="@id"/></Item>
            </xsl:for-each>
        </xsl:variable>
       </xsl:if>

        <xsl:copy-of select="$entityIdList"/>

    </xsl:function>

我在另一个包含常见Function.xslt的xslt文件中调用此函数

<xsl:variable name="entityIdList" select="cdocfx:createEntityIdList(n:report/n:format-inputs)"/>

</xsl:variable>

我的问题是变量entityIdList应该是元素的值类型但是它有文档节点类型我怎么能实现这个?

xslt-2.0
1个回答
0
投票

请提供最小但完整的XML输入示例,您拥有的XSLT,您想要的输出以及您遇到的任何确切错误消息。

我目前还不确定我理解你想要实现什么,如果你构造一个element()*类型的变量,你似乎想要构造一个元素节点序列。但是,任何xsl:value-of只会输出文本节点中所选项的字符串值,因此如果您只想输出字符串值,则不清楚为什么首先构造元素。如果你构造节点并想要输出它们,请使用xsl:copy-ofxsl:sequence,而不是xsl:value-of

为了显示编写一个返回元素序列的函数的两个例子(即其结果是element()*类型),我设置了https://xsltfiddle.liberty-development.net/3NzcBtE,它有两个函数

  <xsl:function name="mf:ex1">
      <xsl:param name="input" as="element()*"/>
      <xsl:for-each select="$input">
          <item>{ @id }</item>
      </xsl:for-each>
  </xsl:function>

  <xsl:function name="mf:ex2">
      <xsl:param name="input" as="element()*"/>
      <xsl:variable name="elements" as="element()*">
          <xsl:for-each select="$input">
              <item>{ @id }</item>
          </xsl:for-each>          
      </xsl:variable>
      <xsl:sequence select="$elements"/>
  </xsl:function>

第一个简单地直接在函数体中构造一些结果元素,这样结果就是一系列元素节点。第二个函数使用你在变量中构造一系列元素节点的方法,然后从函数返回该变量值的正确方法是使用xsl:sequence

目前尚不清楚您认为发布的代码在哪个位置处理document-node()节点。

另请注意

    <xsl:choose>
        <xsl:when test="fn:exists($formatInputsNodes/n:narrative-entity-ids)">
            <xsl:for-each select="$formatInputsNodes/n:narrative-entity-ids/n:entity">
                <Item><xsl:value-of select="@id"/></Item>
            </xsl:for-each>
        </xsl:when>

    </xsl:choose>

可以简化为

      <xsl:for-each select="$formatInputsNodes/n:narrative-entity-ids/n:entity">
             <Item><xsl:value-of select="@id"/></Item>
      </xsl:for-each>

正如您现在提供的XML输入至少是格式良好的,并且一些XSLT片段(遗憾的是,虽然显示的XML输入不使用命名空间,但似乎没有使用命名空间),这里是尝试变形进入工作样本

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:cdocfx="http://example.com/cdox-functions"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:function name="cdocfx:createEntityIdList" >
    <xsl:param name="formatInputsNodes"/>

    <xsl:variable name="entityIdList" as="element()*">
        <xsl:for-each select="$formatInputsNodes/narrative-entity-ids/entity">
            <Item><xsl:value-of select="@id"/></Item>
        </xsl:for-each>
    </xsl:variable>


    <xsl:copy-of select="$entityIdList"/>

  </xsl:function>

  <xsl:variable name="entityIdList" select="cdocfx:createEntityIdList(report/format-inputs)"/>

  <xsl:output method="text"/>

  <xsl:template match="/">
     <xsl:value-of select="$entityIdList instance of element()*, $entityIdList" separator=", "/>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/pPqsHTW/1

检查$entityIdList instance of element()*的输出是true所以我不知道为什么你说你有一个文档节点。

© www.soinside.com 2019 - 2024. All rights reserved.