开发者

XSL namespaces and nested xsl:for-each

I have below XML and would like to iterate through the element as such the i could display it in some format like:

PIN 1<br/>
&nbsp;&nbsp;  XYZ<br/>
&nbsp;&nbsp;  HELLO<br/>

PIN 2<br/>
&nbsp;&nbsp;  ABC<br/>
&nbsp;&nbsp;  HI<br/>

XML:

<RootResponse xmlns:ip="urn:domain:tx:inPayment" xmlns:ipn="urn:domain:tx:Pin">
   <OutBoundMessage>
      <ip:InfoMessage>
        <ipn:Alert>PIN 1</ipn:Alert>
         <ipn:Code>
           <ip:CodeLabel>XYZ</ip:CodeLabel>
           <ip:CodeMessage>HELLO</ip:CodeMessage>
         </ipn:Code>
      </ip:I开发者_如何学GonfoMessage>

      <ip:InfoMessage>
         <ipn:Code>
           <ipn:Alert>PIN 2</ipn:Alert>
           <ip:CodeLabel>ABC</ip:CodeLabel>
           <ip:CodeMessage>HI</ip:CodeMessage>
         </ipn:Code>
      </ip:InfoMessage>
  </OutBoundMessage>
</RootResponse>

I Can't seem to find a solution. Any suggestion?


I would recommend following the W3C schools XSLT tutorial, this should give you all you need to solve this relatively simple XSLT problem.

You are right that you will have to pay attention to namespaces, although again this is quite straightforward. Simply ensure that your XSLT defines the namespaces required, and that you prefix element names in your XPath statements accordingly. See the following:

XML Namespaces and How They Affect XPath and XSLT


You should declare the namespaces in your XSLT and then use the declared prefix in your expressions.

Below is an example of how to do that, using templates(i.e. "push style") rather than xsl:for-each (e.g. "pull style").

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ip="urn:domain:tx:inPayment"
xmlns:ipn="urn:domain:tx:Pin"
exclude-result-prefixes="ip ipn">
    <xsl:output indent="yes" />

    <xsl:template match="ipn:Alert">
        <xsl:text>&#xA;</xsl:text>
        <xsl:apply-templates />
        <br/>
    </xsl:template>

    <xsl:template match="ip:*[starts-with(local-name(),'Code')]">
        <xsl:text>&#xA;&#160;&#160;</xsl:text>
        <xsl:apply-templates/>
        <br/>
    </xsl:template>

</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜