【问题标题】:Extracting multiple XML values using XSLT使用 XSLT 提取多个 XML 值
【发布时间】:2013-05-16 12:49:33
【问题描述】:

我正在开发一些 XSLT 以从复杂的 XML 中提取值。

xml:

   <bean id="timingAdvice" 

class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor" />

<bean id="XMLhandler" class="com.order.OrderStatusSAXHandler">
</bean>

我希望达到的输出:

<bean>
<id>timingAdvice</id>
<class>org.springframework.aop.interceptor.PerformanceMonitorInterceptor</class>
</bean>

<bean>
<id>XMLhandler</id>
<class>com.citi.get.rio.order.OrderStatusSAXHandler</class>
</bean>

我正在使用这个 XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8"
        indent="yes" />
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
        <xsl:template match="beans/bean">
        <xsl:element name="{@class}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
        </xsl:template>
    <xsl:template match="beans/bean">
        <xsl:element name="{@id}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

但是这个输出:

    <?xml version="1.0" encoding="UTF-8"?>
<beans>

<timingAdvice/>
<XMLhandler>
</XMLhandler>
</beans>

这不是我想要的。

我想检查 xml 打印它们的每个属性,如下所示:

<attributeName>value<attributeName>

编辑

我遇到了beans 标记的问题,它包含许多对 Spring 框架的弹簧引用:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
default-lazy-init="false">

当这是开始标记时,提供的解决方案不提供所需的输出。有没有办法在beans 标签中忽略这些引用

【问题讨论】:

  • 说实话只是它的一个标识符,所以意义不大。但是,我已将其更正为 id。

标签: xslt


【解决方案1】:

那么,是这样的吗?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:bn="http://www.springframework.org/schema/beans">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

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

  <xsl:template match="bn:bean/@*">
    <xsl:element name="{name()}" namespace="{namespace-uri(..)}">
      <xsl:value-of select="." />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

在您的示例输入上运行时(当它包含在 &lt;beans&gt; 元素中时),结果是:

<beans xsi:schemaLocation="     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd" default-lazy-init="false" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util">
  <bean>
    <id>timingAdvice</id>
    <class>org.springframework.aop.interceptor.PerformanceMonitorInterceptor</class>
  </bean>

  <bean>
    <id>XMLhandler</id>
    <class>com.order.OrderStatusSAXHandler</class>
  </bean>
</beans>

从属性转换的元素的顺序是否重要,或者它们是否可以与属性以相同的顺序出现?

【讨论】:

  • 是的,这似乎可以解决问题。只要将它们放在 bean 下,顺序无关紧要。我有一些比提供的示例更复杂的 xml,我必须编写 xslt。我可能必须想办法解决这个问题。
  • +1,很好的答案。顺便说一句,输入中属性的顺序是 XSLT 不可用的信息,所以这里的最后一个问题没有实际意义。
  • @LarsH 虽然这在技术上是正确的,但我认为许多 XSLT 处理器将属性视为它们在源文档中出现的顺序。我知道至少 MSXSL 肯定是这种情况。
  • 我不会这么认为,因为规范说不。我很好奇你怎么知道 MSXSL 肯定是这种情况;它是否记录在案,或者您只是在几个实例中观察到它,在这种情况下它可能会在不同的版本或不同的执行环境中发生变化?无论如何,我不反对提出的问题。只是想指出,依赖 XML 属性的顺序是违反 XML Info 模型的。 OP 最好在构建依赖于订单的流程之前了解这一点。
  • @JLRishe 为答案喝彩。我是从 spring 文件中执行此操作的,并且打开的 bean 标记有一些对 spring 框架的引用,我怎么能忽略这些,因为它导致 xslt 无法工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
  • 2011-03-30
  • 2015-01-20
  • 1970-01-01
相关资源
最近更新 更多