【问题标题】:Replace blank Attribute with value in XML with XSLT transformation使用 XSLT 转换将空白属性替换为 XML 中的值
【发布时间】:2018-06-12 07:20:18
【问题描述】:

我有以下 XMl,我需要用动态 GUID 值替换 root=""。

我如何做到这一点?可以在 XML 文档中的任何位置。我不知道如何正确发布。 stackoberflow 不断发出警告,以进一步解释以下问题,然后是代码。所以我正在尝试这样做。

            <ClinicalDocument xmlns="urn:hl7-org:v3">
            <templateId root="2.16.840.1.113883.10.20.22.1.2" extension="2015-08-01"/>
            <id root=""/>
            <code code="34133-9" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LN"
            displayName="Summarization of Episode Note"/>
            <title>Patient Summary Document</title>
            <languageCode code="en-US"/>
            <component>
            <structuredBody>
              <component>
                <section>
                  <templateId root="2.16.840.1.113883.10.20.22.2.6.1"/>   
                  <entry typeCode="DRIV">
                    <act classCode="ACT" moodCode="EVN">
                      <templateId root="2.16.840.1.113883.10.20.22.4.30"/>
                      <templateId root="2.16.840.1.113883.10.20.22.4.30" extension="2015-08-01"/>
                      <id nullFlavor="UNK"/>  
                      <informant>
                        <assignedEntity>
                          <id root="2.16.840.1.113883.3.86.3.1" extension="STHS"/>
                          <addr nullFlavor="UNK"/>
                          <telecom nullFlavor="UNK"/>
                          <assignedPerson>
                            <name nullFlavor="UNK"/>
                          </assignedPerson>
                          <representedOrganization>
                            <id root="" extension="STHS" displayable="true"/>
                            <name>STHS</name>
                            <telecom nullFlavor="UNK"/>
                            <addr nullFlavor="UNK"/>
                          </representedOrganization>
                        </assignedEntity>
                      </informant>
                    </act>
                  </entry>
                </section>
              </component>
            </structuredBody>
            </component>
            </ClinicalDocument>

我从这个帖子的答案中得到了 XSLT。但不知何故它不起作用。

            <?xml version="1.0"?>
            <xsl:stylesheet version="1.0" xmlns:isc="http://extension-functions.intersystems.com" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                <xsl:output omit-xml-declaration="yes" indent="yes"/>

                <xsl:variable name="GUID" select="'FF1122'"/>

                <xsl:template match="id/@root[.='']">
                    <xsl:attribute name="root">
                        <xsl:value-of select="$GUID"/>
                    </xsl:attribute>
                </xsl:template>

                <xsl:template match="@* | node()">
                    <xsl:copy>
                        <xsl:apply-templates select="@* | node()"/>
                    </xsl:copy>
                </xsl:template>


            </xsl:stylesheet>

【问题讨论】:

  • 您需要使用任何一种语言来解析您的 XML。许多语言都有相应的库。试试python 和ElementTree

标签: xml xslt xml-parsing xslt-1.0


【解决方案1】:
<!-- This could be a parm also. -->
<xsl:variable name="GUID" select="'FF1122'"/>

<xsl:template match="id/@root[.='']">
  <xsl:attribute name="root">
    <xsl:value-of select="$GUID"/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

【讨论】:

  • 感谢您的回复。但是,这会将所有根属性替换为 FF1122。我只想替换
  • 只需将 match="@root" 更改为 match="id/@root"。
  • 空白值检查在哪里 root=空白检查。以下不工作
  • 谢谢@Bluewood66。它适用于上面的示例,但不适用于我的实际数据。
  • 首先想到的是你有一个命名空间问题。因此,像 id 这样的名称,在命名空间中时,将需要命名空间来引用它们。有几种方法可以做到这一点。但这只是一个猜测。如果您使用实际的 XML 和您的代码尝试发布另一个问题,那么有人会尝试让您继续前进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 2010-10-13
  • 2018-09-22
  • 1970-01-01
  • 2013-06-29
  • 2021-01-19
相关资源
最近更新 更多