jaxb transformerfactory xml到xml通过xslt使用generate-id获取唯一值

时间:2013-03-19 13:50:43

标签: xslt jaxb

我有以下一些代码可以通过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>

1 个答案:

答案 0 :(得分:0)

  

我决定使用导入Saxon来启用XSLT 2.0。秒后分组是完美的,并且很容易。