簡體   English   中英

基於“名稱”屬性使用 XSLT 重命名 XML 節點

[英]Renaming XML nodes with XSLT based on “name” attribute

我有這個 xml:

<?xml version="1.0" encoding="utf-8"?>
<Document Id="0c744468-67d8-4daa-8ff9-cbd23209c59d" Name="ROW_Easement (1)" TypeId="adde4dc1-0710-452a-82c7-9e5ac1bafe94" TypeName="ROW_Easement" OriginalName="106-19-47A.pdf" MimeType="application/pdf">
  <Field Name="File Name" Confidence="1.00" Page="1" Valid="True">106-19-47A</Field>
  <Section Name="tblPersonOfInterest">
    <SectionCollection Name="From" Count="4">
      <Section Name="From 1">
        <Field Name="Grantor" Confidence="1.00" Page="1" Valid="True" Location="1.713, 8.200, 6.487, 0.500">MARY E. GIBSON, and husband, E. J. GIBSON;
 ROSALIE L. SIEN, and husband, A. C. SIEN, Jr</Field>
      </Section>
     </SectionCollection>
   </Section>
</Document>

我想用“名稱”屬性值替換所有節點名稱。 到目前為止,我有:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="@*|node()">
    <ROW_Easement>
      <xsl:for-each select="Field">
        <xsl:element name="{translate(@Name, ' ', '_')}">
          <xsl:value-of select="self::node()" />
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="Section">
        <xsl:element name="{translate(@Name, ' ', '_')}">
          <xsl:value-of select="self::node()" />
        </xsl:element>
      </xsl:for-each>
      <xsl:for-each select="SectionCollection">
        <xsl:element name="{translate(@Name, ' ', '_')}">
          <xsl:value-of select="self::node()" />
        </xsl:element>
      </xsl:for-each>
    </ROW_Easement>
  </xsl:template>
</xsl:stylesheet>

它導致:

<?xml version="1.0" encoding="utf-8"?>
<ROW_Easement>
  <File_Name>106-19-47A</File_Name>
  <tblPersonOfInterest>
    
      
        MARY E. GIBSON, and husband, E. J. GIBSON;
 ROSALIE L. SIEN, and husband, A. C. SIEN, Jr
      
     
   </tblPersonOfInterest>
</ROW_Easement>

它在 SectionCollection 節點處中斷,但我不明白為什么。

根據@michael.hor257k 的建議更新更新:使用我現在得到的第一個建議:

<?xml version="1.0" encoding="utf-8"?><ROW_Easement>0c744468-67d8-4daa-8ff9-cbd23209c59dROW_Easement (1)adde4dc1-0710-452a-82c7-9e5ac1bafe94ROW_Easement106-19-47A.pdfapplication/pdf
  <File_Name>1.001True106-19-47A</File_Name>
  <tblPersonOfInterest>
    <From>4
      <From_1>
        <Grantor>1.001True1.713, 8.200, 6.487, 0.500MARY E. GIBSON, and husband, E. J. GIBSON;
 ROSALIE L. SIEN, and husband, A. C. SIEN, Jr</Grantor>
      </From_1>
     </From>
   </tblPersonOfInterest>
</ROW_Easement>

假設一個格式良好的XML 輸入,您可以使用這個簡單的 XSLT-3.0 代碼。 它將所有元素的名稱替換為@Name屬性,並且僅從 output 中刪除此屬性。 rest 按原樣由xsl:mode復制:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
  <xsl:mode on-no-match="shallow-copy" />
  
  <xsl:template match="Document">
    <ROW_Easement>
        <xsl:apply-templates select="node()|@*" />
    </ROW_Easement>
  </xsl:template>
    
  <xsl:template match="*">
    <xsl:element name="{translate(@Name, ' ', '_')}">
        <xsl:apply-templates select="node()|@*[local-name()!='Name']" />
    </xsl:element>
  </xsl:template>
  
</xsl:stylesheet>

Output 是:

<?xml version="1.0" encoding="UTF-8"?>
<ROW_Easement Id="0c744468-67d8-4daa-8ff9-cbd23209c59d"
              Name="ROW_Easement (1)"
              TypeId="adde4dc1-0710-452a-82c7-9e5ac1bafe94"
              TypeName="ROW_Easement"
              OriginalName="106-19-47A.pdf"
              MimeType="application/pdf">
   <File_Name Confidence="1.00" Page="1" Valid="True">106-19-47A</File_Name>
   <tblPersonOfInterest>
      <From Count="4">
         <From_1>
            <Grantor>
          </Grantor>
         </From_1>
      </From>
   </tblPersonOfInterest>
</ROW_Easement>

沒有說明您要保留哪些屬性,但根據您的示例輸入和您稍后發布的代碼,您似乎不想保留任何屬性,但Document上的Name one 所以

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">

  <xsl:template match="Document">
    <ROW_Easement Name="{@Name}">
        <xsl:apply-templates/>
    </ROW_Easement>
  </xsl:template>
  
  <xsl:template match="*">
    <xsl:element name="{translate(@Name, ' ', '_')}">
        <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  
</xsl:stylesheet>

可能會更短,並避免列出您不想復制的所有屬性。

它可能不是最優雅的解決方案,但使用@zx485 的回答我將 XSLT 更改為:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
  <xsl:mode on-no-match="shallow-copy" />
  <!-- Remove unnecessary attributes -->
  <xsl:template match="@Confidence" />
  <xsl:template match="@Id" />
  <xsl:template match="@TypeId" />
  <xsl:template match="@TypeName" />
  <xsl:template match="@OriginalName" />
  <xsl:template match="@MimeType" />
  <xsl:template match="@Page" />
  <xsl:template match="@Valid" />
  <xsl:template match="@Count" />
  <xsl:template match="@Location" />
  
  <!-- Rename nodes -->
  <xsl:template match="Document">
    <ROW_Easement>
        <xsl:apply-templates select="node()|@*" />
    </ROW_Easement>
  </xsl:template>
  
  <!-- Copy to new XML -->
  <xsl:template match="*">
    <xsl:element name="{translate(@Name, ' ', '_')}">
        <xsl:apply-templates select="node()|@*[local-name()!='Name']" />
    </xsl:element>
  </xsl:template>
  
</xsl:stylesheet>

現在它可以工作了。 如果有人願意提供改進方法的建議,仍然持開放態度。 謝謝你們。

暫無
暫無

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

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