簡體   English   中英

從 XML 文件中刪除 xmlns 命名空間

[英]Delete xmlns namespace from XML file

我想從XML文件中刪除第一個xmlns屬性,因為它給我的XSL轉換帶來了一些麻煩。

這是原始的 xml :

<ns4:ExportReferences xmlns="urn:be:fgov:ehealth:samws:v2:refdata" xmlns:ns2="urn:be:fgov:ehealth:samws:v2:core" xmlns:ns3="urn:be:fgov:ehealth:samws:v2:consultation" xmlns:ns4="urn:be:fgov:ehealth:samws:v2:export">
  <AtcClassification code="A">
    <Description>Alimentary Tract and Metabolism</Description>
  </AtcClassification>
  <AtcClassification code="A01">
    <Description>Stomatological Preparations</Description>
  </AtcClassification>
</ns4:ExportReferences>

我第一次嘗試使用RegexRegex.IsMatch(xml, @"xmlns=(.*?)")給了我一個 true 但Regex.Replace(xml, pattern, "")不起作用。

然后我嘗試刪除它:

XDocument doc = XDocument.Load(@"file.xml");
foreach (var attr in doc.Descendants().Attributes()){
  if (attr.Name == "xmlns")
    attr.Remove();}

還有很多其他嘗試,但我總是有我不明白的相同行為:我可以刪除第一個xmlns屬性,但是當我保存文件時,它現在出現在每個元素中:

<ns4:ExportReferences xmlns="urn:be:fgov:ehealth:samws:v2:refdata" xmlns:ns2="urn:be:fgov:ehealth:samws:v2:core" xmlns:ns3="urn:be:fgov:ehealth:samws:v2:consultation" xmlns:ns4="urn:be:fgov:ehealth:samws:v2:export">
  <AtcClassification code="A" xmlns="urn:be:fgov:ehealth:samws:v2:refdata">
    <Description>Alimentary Tract and Metabolism</Description>
  </AtcClassification>
  <AtcClassification code="A01" xmlns="urn:be:fgov:ehealth:samws:v2:refdata">
    <Description>Stomatological Preparations</Description>
  </AtcClassification>
</ns4:ExportReferences>

如果我保存它並再次嘗試:

foreach (var attr in doc.Descendants().Attributes()){
  if (attr.NextAttribute != null && attr.NextAttribute.Name == "xmlns")
    attr.NextAttribute.Remove();}

它也將其刪除,但在重新打開文件時沒有進行任何更改。

任何幫助將不勝感激。

編輯提供更多信息

  1. 輸入 XML :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:ExportReferences xmlns="urn:be:fgov:ehealth:samws:v2:refdata" xmlns:ns2="urn:be:fgov:ehealth:samws:v2:core" xmlns:ns3="urn:be:fgov:ehealth:samws:v2:consultation" xmlns:ns4="urn:be:fgov:ehealth:samws:v2:export" xmlns:ns5="urn:be:fgov:ehealth:samws:v2:actual:common" xmlns:ns6="urn:be:fgov:ehealth:samws:v2:company:submit" xmlns:ns7="urn:be:fgov:ehealth:samws:v2:reimbursement:submit" xmlns:ns8="urn:be:fgov:ehealth:samws:v2:reimbursementlaw:submit" xmlns:ns9="urn:be:fgov:ehealth:samws:v2:virtual:common" xmlns:ns10="urn:be:fgov:ehealth:samws:v2:compounding:common" xmlns:ns11="urn:be:fgov:ehealth:samws:v2:nonmedicinal:common" xmlns:ns12="urn:be:fgov:ehealth:samws:v2:chapteriv:submit" xmlns:ns13="urn:be:fgov:ehealth:samws:v2:actual:status" version="5.0" SamId="E.20201119_141847">
    <AtcClassification code="A">
        <Description>Alimentary Tract and Metabolism</Description>
    </AtcClassification>
    <AtcClassification code="A01">
        <Description>Stomatological Preparations</Description>
    </AtcClassification>
    <AtcClassification code="A01A">
        <Description>Stomatological Preparations</Description>
    </AtcClassification>
    <AtcClassification code="A01AA">
        <Description>Caries Prophylactic Agents</Description>
    </AtcClassification>
    <AtcClassification code="A01AA01">
        <Description>Sodium Fluoride</Description>
    </AtcClassification>
    <AtcClassification code="A01AA02">
        <Description>Sodium Monofluorophosphate</Description>
    </AtcClassification>
</ns4:ExportReferences>
  1. XSLT :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns2="urn:be:fgov:ehealth:samws:v2:core"
xmlns:ns3="urn:be:fgov:ehealth:samws:v2:consultation"
xmlns:ns4="urn:be:fgov:ehealth:samws:v2:export"
xmlns:ns5="urn:be:fgov:ehealth:samws:v2:actual:common"
xmlns:ns6="urn:be:fgov:ehealth:samws:v2:company:submit"
xmlns:ns7="urn:be:fgov:ehealth:samws:v2:reimbursement:submit"
xmlns:ns8="urn:be:fgov:ehealth:samws:v2:reimbursementlaw:submit"
xmlns:ns9="urn:be:fgov:ehealth:samws:v2:virtual:common"
xmlns:ns10="urn:be:fgov:ehealth:samws:v2:compounding:common"
xmlns:ns11="urn:be:fgov:ehealth:samws:v2:nonmedicinal:common"
xmlns:ns12="urn:be:fgov:ehealth:samws:v2:chapteriv:submit"
xmlns:ns13="urn:be:fgov:ehealth:samws:v2:actual:status">
<xsl:template match="/ns4:ExportReferences">
  <xsl:for-each select="AtcClassification">
    <xsl:text>INSERT INTO ATC VALUES ('</xsl:text>
      <xsl:value-of select="@code" />
      <xsl:text>','</xsl:text>
        <xsl:value-of select="Description" />
      <xsl:text>');</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
  1. 所需的輸出:
INSERT INTO ATC VALUES ('A','Alimentary Tract and Metabolism');INSERT INTO ATC VALUES ('A01','Stomatological Preparations');INSERT INTO ATC VALUES ('A01A','Stomatological Preparations');INSERT INTO ATC VALUES ('A01AA','Caries Prophylactic Agents');INSERT INTO ATC VALUES ('A01AA01','Sodium Fluoride');INSERT INTO ATC VALUES ('A01AA02','Sodium Monofluorophosphate');
  1. XSLT 處理器及其版本:

xsl:vendor = Microsoft xsl:version = 1

所以你應該只需要將該命名空間添加到你的 XSL 中:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns2="urn:be:fgov:ehealth:samws:v2:core"
xmlns:ns3="urn:be:fgov:ehealth:samws:v2:consultation"
xmlns:ns4="urn:be:fgov:ehealth:samws:v2:export"
xmlns:ns5="urn:be:fgov:ehealth:samws:v2:actual:common"
xmlns:ns6="urn:be:fgov:ehealth:samws:v2:company:submit"
xmlns:ns7="urn:be:fgov:ehealth:samws:v2:reimbursement:submit"
xmlns:ns8="urn:be:fgov:ehealth:samws:v2:reimbursementlaw:submit"
xmlns:ns9="urn:be:fgov:ehealth:samws:v2:virtual:common"
xmlns:ns10="urn:be:fgov:ehealth:samws:v2:compounding:common"
xmlns:ns11="urn:be:fgov:ehealth:samws:v2:nonmedicinal:common"
xmlns:ns12="urn:be:fgov:ehealth:samws:v2:chapteriv:submit"
xmlns:ns13="urn:be:fgov:ehealth:samws:v2:actual:status"

xmlns:something="urn:be:fgov:ehealth:samws:v2:refdata">

<xsl:template match="/ns4:ExportReferences">

  <xsl:for-each select="something:AtcClassification">

    <xsl:text>INSERT INTO ATC VALUES ('</xsl:text>
      <xsl:value-of select="@code" />
      <xsl:text>','</xsl:text>

        <xsl:value-of select="something:Description" />

      <xsl:text>');</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

注意: ActClassification元素在默認命名空間中,因為它在 XML 中沒有前綴。 由於它位於另一個將默認命名空間聲明為urn:be:fgov:ehealth:samws:v2:refdata ,所以它就是這樣。

另請注意,命名空間前綴具有本地含義。 有沒有必要為你使用ns2在您的轉換只是因為XML文件使用ns2 ; 這是重要/需要匹配的骨灰盒。 因此,我在這里urn:be:fgov:ehealth:samws:v2:refdata了前綴為somethingurn:be:fgov:ehealth:samws:v2:refdata

我似乎不需要 XSLT、動態 SQL 或其他任何東西。

可以直接在 SQL Server 中的數據庫端執行此操作。 在那里處理命名空間非常容易。

查詢語句

DECLARE @tbl_atc TABLE (code VARCHAR(10), Description VARCHAR(100));
DECLARE @xml XML =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:ExportReferences xmlns="urn:be:fgov:ehealth:samws:v2:refdata"
                      xmlns:ns2="urn:be:fgov:ehealth:samws:v2:core"
                      xmlns:ns3="urn:be:fgov:ehealth:samws:v2:consultation"
                      xmlns:ns4="urn:be:fgov:ehealth:samws:v2:export"
                      xmlns:ns5="urn:be:fgov:ehealth:samws:v2:actual:common"
                      xmlns:ns6="urn:be:fgov:ehealth:samws:v2:company:submit"
                      xmlns:ns7="urn:be:fgov:ehealth:samws:v2:reimbursement:submit"
                      xmlns:ns8="urn:be:fgov:ehealth:samws:v2:reimbursementlaw:submit"
                      xmlns:ns9="urn:be:fgov:ehealth:samws:v2:virtual:common"
                      xmlns:ns10="urn:be:fgov:ehealth:samws:v2:compounding:common"
                      xmlns:ns11="urn:be:fgov:ehealth:samws:v2:nonmedicinal:common"
                      xmlns:ns12="urn:be:fgov:ehealth:samws:v2:chapteriv:submit"
                      xmlns:ns13="urn:be:fgov:ehealth:samws:v2:actual:status"
                      version="5.0" SamId="E.20201119_141847">
    <AtcClassification code="A">
        <Description>Alimentary Tract and Metabolism</Description>
    </AtcClassification>
    <AtcClassification code="A01">
        <Description>Stomatological Preparations</Description>
    </AtcClassification>
    <AtcClassification code="A01A">
        <Description>Stomatological Preparations</Description>
    </AtcClassification>
    <AtcClassification code="A01AA">
        <Description>Caries Prophylactic Agents</Description>
    </AtcClassification>
    <AtcClassification code="A01AA01">
        <Description>Sodium Fluoride</Description>
    </AtcClassification>
    <AtcClassification code="A01AA02">
        <Description>Sodium Monofluorophosphate</Description>
    </AtcClassification>
</ns4:ExportReferences>';

;WITH XMLNAMESPACES(DEFAULT 'urn:be:fgov:ehealth:samws:v2:refdata'
    , 'urn:be:fgov:ehealth:samws:v2:export' AS ns4) --, rs AS
INSERT INTO @tbl_atc
SELECT c.value('@code', 'VARCHAR(30)') AS code
    , c.value('(Description/text())[1]', 'VARCHAR(100)') AS [Description]
FROM @xml.nodes('/ns4:ExportReferences/AtcClassification') AS t(c);

-- test
SELECT * FROM @tbl_atc;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM