Reputation: 551
I have a XML with an XML string inside
<?xml version="1.0" encoding="UTF-8"?><body xmlns:ns0="http://www.ibm.com/websphere/sibx/ServiceGateway" xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema">
<ns0:JMSTextBody_element>
<ns1:value><delivery gduid="6395" sduid="2392">
<object type="issue" id="C0.ISS.1451" alt_id="BBG000BH7B07">
<attributes>
<simple id="bb_global_id" changed="true">BBG000BH7B07</simple>
<simple id="dv_source1" changed="true">BB</simple>
<simple id="nominal_value" changed="true">0.1</simple>
</attributes>
</object>
</delivery></ns1:value>
</ns0:JMSTextBody_element>
</body>
I need to fetch the values from the inline XML reformat and create a new XML string
<?xml version="1.0" encoding="UTF-8"?><body xmlns:ns0="http://www.ibm.com/websphere/sibx/ServiceGateway" xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema">
<ns0:JMSTextBody_element>
<ns1:value>
<ac_connect_type>issue</ac_connect_type><ac_connect_id>C0.ISS.1451</ac_connect_id><ac_connect_alt_id>BBG000BH7B07</ac_connect_alt_id><bb_global_id>BBG000BH7B07</bb_global_id>
<dv_source1>BB</dv_source1><nominal_value>0.1</nominal_value></ns1:value>
</ns0:JMSTextBody_element>
</body>
I'm having trouble to get to the elements in the inline XML
<xsl:apply-templates select="/delivery/object/attributes/bb_global_id" />
doesn't seem to work, obviously I'm missing something here Thanks
Upvotes: 0
Views: 6944
Reputation: 116959
obviously I'm missing something here
It seems you're missing the fact that the escaped XML inside ns1:value
is just a meaningless string, and cannot be addressed using XPath.
The "correct" solution is to make the transformation in two passes. In the first pass, use:
XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://www.ibm.com/websphere/sibx/ServiceGateway" xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/body">
<xsl:value-of select="ns0:JMSTextBody_element/ns1:value" disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
Save the result of this transformation to a file, then apply another transformation, using another stylesheet, to that file.
Alternatively, you could try and extract the required information from the escaped string using string functions - which is awkward and error-prone.
Upvotes: 1
Reputation: 167401
Assuming Saxon 9.6 and XSLT 3.0 you can use
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema"
exclude-result-prefixes="xs">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns1:value">
<xsl:copy>
<xsl:apply-templates select="parse-xml(.)//object"/>
</xsl:copy>
</xsl:template>
<xsl:template match="object">
<xsl:apply-templates select="@* , attributes/simple"/>
</xsl:template>
<xsl:template match="object/@*">
<xsl:element name="ac_connect_{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="simple">
<xsl:element name="{@id}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
which creates the output
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:ns0="http://www.ibm.com/websphere/sibx/ServiceGateway"
xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema">
<ns0:JMSTextBody_element>
<ns1:value>
<ac_connect_type>issue</ac_connect_type>
<ac_connect_id>C0.ISS.1451</ac_connect_id>
<ac_connect_alt_id>BBG000BH7B07</ac_connect_alt_id>
<bb_global_id>BBG000BH7B07</bb_global_id>
<dv_source1>BB</dv_source1>
<nominal_value>0.1</nominal_value>
</ns1:value>
</ns0:JMSTextBody_element>
</body>
Upvotes: 2