1

我有以下代码通过 xslt 将一个 xml 文档转换为另一个

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(new javax.xml.transform.stream.StreamSource(
                this.getClass().getClassLoader().getResourceAsStream(ie.getXslFileName())));
        transformer.transform(
                new javax.xml.transform.stream.StreamSource(reader),
                new javax.xml.transform.stream.StreamResult(stream));
        output.put(fileName, stream.toByteArray());

如果我不尝试选择不同的值,它会完美运行。但是,只要我输入 generate-id 行并键入它就会失败。在我使用的所有其他测试工具中,转换是完美的,所以我认为这是 jaxb 转换器的限制?有没有人对我如何选择可以在这里工作并启用 jaxb 转换的唯一值有任何建议?或者,是否有另一个我可以使用的变压器来解决问题?

XSLT 无处不在,但在这里:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" />

  <xsl:key name="groupKey"
           match="//questionGroup/externalCodes/externalCode/externalCode/text()" 
           use="." />

  <xsl:template match="/">
    <xsl:apply-templates select="submissionReport"/>
  </xsl:template>

  <xsl:template match="submissionReport" >
    <component>
      <structuredBody>
        <xsl:for-each select="//questionResponse/question/questionGroup
                               /externalCodes/externalCode[governingBody
                                [internalCode='GVB-AHRQ-1']]
                               /externalCode/text()[generate-id() = 
                                  generate-id(key('groupKey',.)[1])]">
          <component>
            <section>
              <entry>
                <templateId>
                  <xsl:attribute name="root">
                    <xsl:value-of select="."/>
                  </xsl:attribute>
                </templateId>
                <organizer classCode="CLUSTER" moodCode="EVN">
                  <id nullFlavor="NA"/>
                  <statusCode code="completed"/>
                  <xsl:call-template name="groupedResponses">
                    <xsl:with-param name="groupInternalCode" select="."/>
                  </xsl:call-template>
                </organizer>
              </entry>
            </section>
          </component>
        </xsl:for-each>
      </structuredBody>
    </component>
  </xsl:template>

  <xsl:template name="groupedResponses">
    <xsl:param name="groupInternalCode"/>
    <xsl:for-each select="//questionResponse[question[externalCodes
                            [externalCode[governingBody[internalCode='GVB-AHRQ-1'] and 
                               not(externalCode='DE42') and not(externaCode='DE3') and 
                               not(externalCode='DE46') and not(externalCode='DE49') and 
                               not(externalCode='DE30')]] and 
                               questionGroup[externalCodes[externalCode[governingBody[internalCode='GVB-AHRQ-1'] and 
                               externalCode=$groupInternalCode]]]]]">
      <component>
        <observation>
          <xsl:attribute name="classCode">OBS</xsl:attribute>
          <xsl:attribute name="moodCode">EVN</xsl:attribute>
          <templateId>
            <xsl:attribute name="root">2.16.840.1.113883.3.263.1.11.3.100</xsl:attribute>
          </templateId>
          <xsl:for-each select="question/externalCodes/externalCode
                                   [governingBody[internalCode='GVB-AHRQ-1'] or 
                                   governingBody[internalCode='GVB-CDCRACE-1'] or 
                                   governingBody[internalCode='GVB-HL7NULL-1'] or 
                                   governingBody[internalCode='GVB-HL7GENDER-1']]">
            <!-- Question information -->
            <code>
              <xsl:attribute name="code">
                <xsl:value-of select="externalCode"/>
              </xsl:attribute>
              <xsl:attribute name="displayName">
                <xsl:value-of select="../../text"/>
              </xsl:attribute>
              <xsl:attribute name="codeSystem">
                <xsl:value-of select="governingBody/externalCode"/>
              </xsl:attribute>
              <xsl:attribute name="codeSystemName">
                <xsl:value-of select="governingBody/name"/>
              </xsl:attribute>
            </code>

            <!-- Response information.  Can be more than one -->
            <xsl:for-each select="../../../response">
              <value>
                <xsl:choose>
                  <xsl:when test="responseOption">
                    <xsl:for-each select="responseOption/externalCodes/externalCode">
                      <value>
                        <xsl:attribute name="type">CD</xsl:attribute>
                        <xsl:attribute name="code">
                          <xsl:value-of select="externalCode"/>
                        </xsl:attribute>
                        <xsl:attribute name="displayName">
                          <xsl:value-of select="../../response"/>
                        </xsl:attribute>
                        <xsl:attribute name="codeSystem">
                          <xsl:value-of select="governingBody/externalCode"/>
                        </xsl:attribute>
                        <xsl:attribute name="codeSystemName">
                          <xsl:value-of select="governingBody/name"/>
                        </xsl:attribute>
                      </value>
                    </xsl:for-each>
                  </xsl:when>

                  <xsl:otherwise>
                    <value>
                      <xsl:attribute name="type">ED</xsl:attribute>
                      <xsl:attribute name="mediaType">text/plain</xsl:attribute>
                      <xsl:value-of select="response"/>
                    </value>
                  </xsl:otherwise>
                </xsl:choose>
              </value>
            </xsl:for-each>
          </xsl:for-each>
        </observation>
      </component>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
4

1 回答 1

0

我决定导入 Saxon 以启用 XSLT 2.0。几秒钟后的分组是完美的并且很容易工作。

于 2014-05-09T00:31:35.463 回答