You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用XSLT替换XML中空root属性为动态GUID值

Fixing Empty root Attribute Replacement with Dynamic GUID in XSLT

Let's walk through why your current XSLT isn't working and fix it to meet your needs:

1. The Core Problem: Missing Namespace Handling

Your XML uses a default namespace urn:hl7-org:v3, but your XSLT doesn't account for this. In XSLT 1.0, unprefixed element names (like your match="id/@root") won't match elements that belong to a default namespace. That's why your template isn't targeting the empty root attributes at all.

2. Static vs. Dynamic GUID

Your current code uses a hardcoded GUID (FF1122), but you need a dynamically generated one. Since you're already referencing InterSystems extension functions, we can leverage isc:UUID() to create proper unique GUIDs.

Corrected XSLT Code

Here's the updated XSLT that addresses both issues:

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

    <!-- Target ANY empty root attribute across all elements -->
    <xsl:template match="@root[.='']">
        <xsl:attribute name="root">
            <!-- Generate dynamic unique GUID -->
            <xsl:value-of select="isc:UUID()"/>
        </xsl:attribute>
    </xsl:template>

    <!-- Identity template: copies all other nodes/attributes unchanged -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Key Changes Explained

  • Namespace Binding: Added xmlns:hl7="urn:hl7-org:v3" to properly recognize elements from your XML's default namespace. Even though we're matching the attribute directly, this ensures the processor correctly parses all elements in the document.
  • Broad Attribute Matching: Changed the template to match="@root[.='']" so it targets every empty root attribute in the document, not just those on <id> elements (aligning with your request to replace empty root values anywhere).
  • Dynamic GUID Generation: Replaced the hardcoded GUID with isc:UUID() to generate unique, dynamic GUIDs each time the transformation runs.

What to Expect

When you run this XSLT against your XML, both empty root attributes will be replaced with unique GUIDs. For example:

  • <id root=""/> will become something like <id root="a1b2c3d4-5678-90ef-ghij-klmnopqrstuv"/>
  • The <id root="" extension="STHS"/> element will get a different unique GUID in its root attribute.

内容的提问来源于stack exchange,提问作者Nishant Makwana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:52:02