Will
Will

Reputation: 8631

Extracting multiple XML values using XSLT

I'm working on some XSLT to extract the values from complex XML.

The xml:

   <bean id="timingAdvice" 

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

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

The output I wish to achieve:

<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>

I am using this 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>

However this outputs:

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

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

Which is not what I am looking for.

I am want to inspect each attribute of the xml print them like the following:

<attributeName>value<attributeName>

EDIT

I've encountered the problem with the beans tag it holds a number of spring references to the Spring Framework:

<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">

The solution provided doesn't provide the required output when this is the opening tag. Is there a way to ignore these references within the beans tag

Upvotes: 1

Views: 404

Answers (1)

JLRishe
JLRishe

Reputation: 101700

So, something like this?

<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>

When this is run on your sample input (when it's wrapped in a <beans> element), the result is:

<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>

Does the order of the elements converted from attributes matter, or can they occur in the same order as the attributes?

Upvotes: 1

Related Questions