1

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

1
  • to be honest it's just an identifier for it, so it's not of great significance. However, I have corrected it to id. Commented May 16, 2013 at 13:09

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

8 Comments

Yeah that seems to do the trick.The order doesn't matter as long as they are placed under bean. I have some more complex xml than the sample provided which I must write the xslt around. I may have to work out a way to navigate through that.
+1, good answer. BTW the order of the attributes in the input is information that's not available to XSLT, so the final question here is pretty well moot.
@LarsH While that is technically true, I would presume that a lot of XSLT processors treat attributes as being in the order that they occur in the source document. I know this is definitely the case for MSXSL at least.
I wouldn't presume that, since the spec says not to. I'm curious how you know this is definitely the case for MSXSL; is it documented, or have you just observed it in several instances, in which case it could change in different versions or different execution environments? In any case, I don't object to the question being asked; just wanted to point out that relying on the order of XML attributes is contrary to the XML Info model. The OP is well-served to be aware of that before building a process that relies on the order.
@JLRishe Cheers for the answer. I am doing this from spring files and the opening bean tag has some references to the spring framework, how can I ignore these as it's causing the xslt not to work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.