如何使用 XLST 从 XML 中删除某些属性

发布于 2024-11-10 13:01:25 字数 1198 浏览 0 评论 0原文

我通过 Web 服务返回了一个 XML 文档。

<Kronos_WFC encoding="ASCII" version="1.0" WFCVersion="6.1" TimeStamp="01/5/2011 8:38AM">
  <Response Status="Success" Timeout="1800" PersonKey="-1" Object="System" Username="1" Action="Logon" PersonNumber="1">
  </Response>
  <Response Status="Success" action="Load">
      <ScheduleGroup ScheduleGroupName="SomeName" AllowsInheritance="false" AllowContract="false" IsEmploymentTerm="false" />
      <ScheduleGroup ScheduleGroupName="GreatName" AllowsInheritance="true" AllowContract="false" IsEmploymentTerm="false" />
      <ScheduleGroup ScheduleGroupName="BestName" AllowsInheritance="true" AllowContract="false" IsEmploymentTerm="false" />
  </Response>
  <Response Status="Success" Object="System" Action="Logoff">
  </Response>
</Kronos_WFC>

问题是我将结果转换为从该产品的 xsd 模式生成的业务对象 (xsd2code)。该产品在属性架构中没有任何内容(对于 Response):

  • Timeout
  • PersonKey
  • Object
  • Username

我想执行以下操作:

  • 删除上述属性
  • 将所有其他属性转换为元素,包括所有子属性,并且孩子的孩子等等。

我如何使用 XLST 来做到这一点。使用正则表达式删除不需要的属性会更简单吗?

I have an XML document returned to me via a web service.

<Kronos_WFC encoding="ASCII" version="1.0" WFCVersion="6.1" TimeStamp="01/5/2011 8:38AM">
  <Response Status="Success" Timeout="1800" PersonKey="-1" Object="System" Username="1" Action="Logon" PersonNumber="1">
  </Response>
  <Response Status="Success" action="Load">
      <ScheduleGroup ScheduleGroupName="SomeName" AllowsInheritance="false" AllowContract="false" IsEmploymentTerm="false" />
      <ScheduleGroup ScheduleGroupName="GreatName" AllowsInheritance="true" AllowContract="false" IsEmploymentTerm="false" />
      <ScheduleGroup ScheduleGroupName="BestName" AllowsInheritance="true" AllowContract="false" IsEmploymentTerm="false" />
  </Response>
  <Response Status="Success" Object="System" Action="Logoff">
  </Response>
</Kronos_WFC>

The problem is I turn the results into business objects generated from the xsd schema for this product (xsd2code). The product has nothing in the schema for attributes (for Response):

  • Timeout
  • PersonKey
  • Object
  • Username

I would like to do the following:

  • Remove the above mentioned attributes
  • Turn all other attributes into elements, including all children, and the children's childrens etc.

How do I do this using XLST. Would it be simpler to remove the unwanted attributes using Regex?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花坊 2024-11-17 13:01:25

删除是不是更简单
使用正则表达式不需要的属性?

不,这是一个非常简单的 XSLT 操作:

此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match=
 "Response/@*[contains('|Timeout|PersonKey|Object|Username|',
                      concat('|',name(),'|')
                      )
            ]"/>
 <xsl:template match="@*">
  <xsl:element name="{name()}">
   <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<Kronos_WFC encoding="ASCII" version="1.0"
WFCVersion="6.1" TimeStamp="01/5/2011 8:38AM">
    <Response Status="Success" Timeout="1800" PersonKey="-1"
    Object="System" Username="1" Action="Logon"
    PersonNumber="1"></Response>
    <Response Status="Success" action="Load">
        <ScheduleGroup ScheduleGroupName="SomeName"
        AllowsInheritance="false" AllowContract="false"
        IsEmploymentTerm="false" />
        <ScheduleGroup ScheduleGroupName="GreatName"
        AllowsInheritance="true" AllowContract="false"
        IsEmploymentTerm="false" />
        <ScheduleGroup ScheduleGroupName="BestName"
        AllowsInheritance="true" AllowContract="false"
        IsEmploymentTerm="false" />
    </Response>
    <Response Status="Success" Object="System"
    Action="Logoff"></Response>
</Kronos_WFC>

准确产生所需的正确结果

<Kronos_WFC>
   <encoding>ASCII</encoding>
   <version>1.0</version>
   <WFCVersion>6.1</WFCVersion>
   <TimeStamp>01/5/2011 8:38AM</TimeStamp>
   <Response>
      <Status>Success</Status>
      <Action>Logon</Action>
      <PersonNumber>1</PersonNumber>
   </Response>
   <Response>
      <Status>Success</Status>
      <action>Load</action>
      <ScheduleGroup>
         <ScheduleGroupName>SomeName</ScheduleGroupName>
         <AllowsInheritance>false</AllowsInheritance>
         <AllowContract>false</AllowContract>
         <IsEmploymentTerm>false</IsEmploymentTerm>
      </ScheduleGroup>
      <ScheduleGroup>
         <ScheduleGroupName>GreatName</ScheduleGroupName>
         <AllowsInheritance>true</AllowsInheritance>
         <AllowContract>false</AllowContract>
         <IsEmploymentTerm>false</IsEmploymentTerm>
      </ScheduleGroup>
      <ScheduleGroup>
         <ScheduleGroupName>BestName</ScheduleGroupName>
         <AllowsInheritance>true</AllowsInheritance>
         <AllowContract>false</AllowContract>
         <IsEmploymentTerm>false</IsEmploymentTerm>
      </ScheduleGroup>
   </Response>
   <Response>
      <Status>Success</Status>
      <Action>Logoff</Action>
   </Response>
</Kronos_WFC>

说明:

  1. 身份规则/模板“按原样”复制每个节点。

  2. 覆盖与名称为 TimeoutPersonKeyObjectUsername 的任何属性匹配的身份规则的模板> 正文为空,并且不复制它们(从输出中“删除”它们)

  3. 匹配任何属性的模板创建一个元素,其名称是匹配属性的名称,其文本node-child 是匹配属性的值。

记住:使用和覆盖身份规则 是最基本、最强大的 XSLT 设计模式。

Would it be simpler to remove the
unwanted attributes using Regex?

No, this is a very simple XSLT operation:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match=
 "Response/@*[contains('|Timeout|PersonKey|Object|Username|',
                      concat('|',name(),'|')
                      )
            ]"/>
 <xsl:template match="@*">
  <xsl:element name="{name()}">
   <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<Kronos_WFC encoding="ASCII" version="1.0"
WFCVersion="6.1" TimeStamp="01/5/2011 8:38AM">
    <Response Status="Success" Timeout="1800" PersonKey="-1"
    Object="System" Username="1" Action="Logon"
    PersonNumber="1"></Response>
    <Response Status="Success" action="Load">
        <ScheduleGroup ScheduleGroupName="SomeName"
        AllowsInheritance="false" AllowContract="false"
        IsEmploymentTerm="false" />
        <ScheduleGroup ScheduleGroupName="GreatName"
        AllowsInheritance="true" AllowContract="false"
        IsEmploymentTerm="false" />
        <ScheduleGroup ScheduleGroupName="BestName"
        AllowsInheritance="true" AllowContract="false"
        IsEmploymentTerm="false" />
    </Response>
    <Response Status="Success" Object="System"
    Action="Logoff"></Response>
</Kronos_WFC>

produces exactly the wanted, correct result:

<Kronos_WFC>
   <encoding>ASCII</encoding>
   <version>1.0</version>
   <WFCVersion>6.1</WFCVersion>
   <TimeStamp>01/5/2011 8:38AM</TimeStamp>
   <Response>
      <Status>Success</Status>
      <Action>Logon</Action>
      <PersonNumber>1</PersonNumber>
   </Response>
   <Response>
      <Status>Success</Status>
      <action>Load</action>
      <ScheduleGroup>
         <ScheduleGroupName>SomeName</ScheduleGroupName>
         <AllowsInheritance>false</AllowsInheritance>
         <AllowContract>false</AllowContract>
         <IsEmploymentTerm>false</IsEmploymentTerm>
      </ScheduleGroup>
      <ScheduleGroup>
         <ScheduleGroupName>GreatName</ScheduleGroupName>
         <AllowsInheritance>true</AllowsInheritance>
         <AllowContract>false</AllowContract>
         <IsEmploymentTerm>false</IsEmploymentTerm>
      </ScheduleGroup>
      <ScheduleGroup>
         <ScheduleGroupName>BestName</ScheduleGroupName>
         <AllowsInheritance>true</AllowsInheritance>
         <AllowContract>false</AllowContract>
         <IsEmploymentTerm>false</IsEmploymentTerm>
      </ScheduleGroup>
   </Response>
   <Response>
      <Status>Success</Status>
      <Action>Logoff</Action>
   </Response>
</Kronos_WFC>

Explanation:

  1. The identity rule/template copies every node "as-is".

  2. The template overriding the identity rule that matches any attribute with name Timeout, PersonKey, Object, or Username has empty body and doesn't copy them ("deletes" them from the output)

  3. The template matching any attribute creates an element whose name is the name of the matched attribute and whose text node-child is the value of the matched attribute.

Remember: Using and overriding the identity rule is the most fundamental and powerful XSLT design pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文