循环遍历 xsl 中的元素但无法以所需的方式显示它
我试图显示下面提到的 xml 元素:
<root>
<LogException>
<exceptionNotificationGroup>Person</exceptionNotificationGroup>
<exceptionType>Implimentation Test</exceptionType>
<createdDatetime>2011-03-29</createdDatetime>
<exceptionSource>INFINITY</exceptionSource>
<exceptionSourceReferenceId>2345610</exceptionSourceReferenceId>
<exceptionTarget>RMB</exceptionTarget>
<sourceSystem>ODS</sourceSystem>
<message>Mandatory Field Missing-Test</message>
</LogException>
<LogException>
<exceptionNotificationGroup>Vinayak</exceptionNotificationGroup>
<exceptionType>Implimentation Testing</exceptionType>
<createdDatetime>2012-03-29</createdDatetime>
<exceptionSource>INFINITY Check</exceptionSource>
<exceptionSourceReferenceId>2345610</exceptionSourceReferenceId>
<exceptionTarget>ORMB</exceptionTarget>
<sourceSystem>ODS</sourceSystem>
<message>Mandatory Field Missing-Test</message>
</LogException>
</root>
采用下面给出的格式:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:LogException>
<tem:exceptionNotificationGroup>Person</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Test</tem:exceptionType>
<tem:createdDatetime>2011-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>RMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
</tem:LogException>
<tem:LogException>
<tem:exceptionNotificationGroup>Vinayak</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Testing</tem:exceptionType>
<tem:createdDatetime>2012-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY Check</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>ORMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
</tem:LogException>
</soapenv:Body>
</soapenv:Envelope>
通过使用 xslt,我尝试了很多使用不同类型的循环,但没有得到正确的结果。
这是我的 xslt:
<?xml version="1.0" encoding="UTF-8" ?>
<!-- New document created with EditiX at Fri Apr 29 09:54:32 IST 2011 -->
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs xdt err fn" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:err="http://www.w3.org/2005/xqt-errors" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="ns-prefix" select="'soapenv'"/>
<xsl:param name="ns-namespace" select="'http://schemas.xmlsoap.org/soap/envelope/'"/>
<xsl:param name="ns-prefix2" select="'tem'"/>
<xsl:param name="ns-namespace2" select="'http://tempuri.org/'"/>
<xsl:template match="//LogException"><!-- create child element -->
<xsl:variable name="checkCount">
<xsl:value-of select="count(//LogException)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$checkCount<2">
<xsl:element name="{$ns-prefix2}:LogException" namespace="{$ns-namespace2}">
<xsl:for-each select="//LogException/*">
<xsl:variable name="elementname">
<xsl:value-of select="name()"/>
</xsl:variable>
<xsl:element name="{$ns-prefix2}:{$elementname}" namespace="{$ns-namespace2}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:when>
<xsl:otherwise><!--xsl:for-each select="//LogException"-->
<xsl:variable name="loop">
<xsl:value-of select="number($checkCount)-1"/>
</xsl:variable>
<xsl:text>-----Checking the value of the total no. of LogException node-------</xsl:text>
<xsl:value-of select="$checkCount"/>
<xsl:text>--------Value checked------</xsl:text>
<xsl:call-template name="incrementValue">
<xsl:with-param name="value">
<xsl:value-of select="number($loop)"/>
</xsl:with-param>
<xsl:with-param name="limit" select="number($checkCount)"/>
</xsl:call-template><!--xsl:if test="number($checkCount)>0"--><!--/xsl:if--><!--/xsl:for-each-->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="incrementValue">
<xsl:param name="value"/>
<xsl:param name="limit"/>
<xsl:if test="number($value)<number($limit)">
<xsl:text>-----inside condition----------</xsl:text>
<xsl:value-of select="position()"/>
<xsl:element name="{$ns-prefix2}:LogException" namespace="{$ns-namespace2}">
<xsl:for-each select="//LogException[position()]/*">
<xsl:variable name="elementname">
<xsl:value-of select="name()"/>
</xsl:variable>
<xsl:element name="{$ns-prefix2}:{$elementname}" namespace="{$ns-namespace2}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
<xsl:call-template name="incrementValue">
<xsl:with-param name="value" select="$value + 1"/>
<xsl:with-param name="limit" select="$limit"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="/">
<xsl:element name="soapenv:Envelope" namespace="{$ns-namespace}">
<xsl:copy-of select="document('')/*/namespace::*[name()='soapenv' or name()='tem']"/>
<xsl:element name="{$ns-prefix}:Header" namespace="{$ns-namespace}"/>
<xsl:element name="{$ns-prefix}:Body" namespace="{$ns-namespace}">
<xsl:apply-templates select="//LogException"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我得到这样的结果:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
-----Checking the value of the total no. of LogException node-------2--------Value checked-----------inside condition----------1
<tem:LogException>
<tem:exceptionNotificationGroup>Person</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Test</tem:exceptionType>
<tem:createdDatetime>2011-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>RMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
<tem:exceptionNotificationGroup>Vinayak</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Testing</tem:exceptionType>
<tem:createdDatetime>2012-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY Check</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>ORMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
</tem:LogException>
-----Checking the value of the total no. of LogException node-------2--------Value checked-----------inside condition----------2
<tem:LogException>
<tem:exceptionNotificationGroup>Person</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Test</tem:exceptionType>
<tem:createdDatetime>2011-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>RMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
<tem:exceptionNotificationGroup>Vinayak</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Testing</tem:exceptionType>
<tem:createdDatetime>2012-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY Check</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>ORMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
</tem:LogException>
</soapenv:Body>
</soapenv:Envelope>
有人对这个问题有答案吗?
I am trying to display the elements of the xml mentioned below:
<root>
<LogException>
<exceptionNotificationGroup>Person</exceptionNotificationGroup>
<exceptionType>Implimentation Test</exceptionType>
<createdDatetime>2011-03-29</createdDatetime>
<exceptionSource>INFINITY</exceptionSource>
<exceptionSourceReferenceId>2345610</exceptionSourceReferenceId>
<exceptionTarget>RMB</exceptionTarget>
<sourceSystem>ODS</sourceSystem>
<message>Mandatory Field Missing-Test</message>
</LogException>
<LogException>
<exceptionNotificationGroup>Vinayak</exceptionNotificationGroup>
<exceptionType>Implimentation Testing</exceptionType>
<createdDatetime>2012-03-29</createdDatetime>
<exceptionSource>INFINITY Check</exceptionSource>
<exceptionSourceReferenceId>2345610</exceptionSourceReferenceId>
<exceptionTarget>ORMB</exceptionTarget>
<sourceSystem>ODS</sourceSystem>
<message>Mandatory Field Missing-Test</message>
</LogException>
</root>
in the format given below :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:LogException>
<tem:exceptionNotificationGroup>Person</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Test</tem:exceptionType>
<tem:createdDatetime>2011-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>RMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
</tem:LogException>
<tem:LogException>
<tem:exceptionNotificationGroup>Vinayak</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Testing</tem:exceptionType>
<tem:createdDatetime>2012-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY Check</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>ORMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
</tem:LogException>
</soapenv:Body>
</soapenv:Envelope>
by using xslt I tried a lot using different kinds of looping but I din't get the right result.
Here is my xslt:
<?xml version="1.0" encoding="UTF-8" ?>
<!-- New document created with EditiX at Fri Apr 29 09:54:32 IST 2011 -->
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs xdt err fn" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:err="http://www.w3.org/2005/xqt-errors" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="ns-prefix" select="'soapenv'"/>
<xsl:param name="ns-namespace" select="'http://schemas.xmlsoap.org/soap/envelope/'"/>
<xsl:param name="ns-prefix2" select="'tem'"/>
<xsl:param name="ns-namespace2" select="'http://tempuri.org/'"/>
<xsl:template match="//LogException"><!-- create child element -->
<xsl:variable name="checkCount">
<xsl:value-of select="count(//LogException)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$checkCount<2">
<xsl:element name="{$ns-prefix2}:LogException" namespace="{$ns-namespace2}">
<xsl:for-each select="//LogException/*">
<xsl:variable name="elementname">
<xsl:value-of select="name()"/>
</xsl:variable>
<xsl:element name="{$ns-prefix2}:{$elementname}" namespace="{$ns-namespace2}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:when>
<xsl:otherwise><!--xsl:for-each select="//LogException"-->
<xsl:variable name="loop">
<xsl:value-of select="number($checkCount)-1"/>
</xsl:variable>
<xsl:text>-----Checking the value of the total no. of LogException node-------</xsl:text>
<xsl:value-of select="$checkCount"/>
<xsl:text>--------Value checked------</xsl:text>
<xsl:call-template name="incrementValue">
<xsl:with-param name="value">
<xsl:value-of select="number($loop)"/>
</xsl:with-param>
<xsl:with-param name="limit" select="number($checkCount)"/>
</xsl:call-template><!--xsl:if test="number($checkCount)>0"--><!--/xsl:if--><!--/xsl:for-each-->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="incrementValue">
<xsl:param name="value"/>
<xsl:param name="limit"/>
<xsl:if test="number($value)<number($limit)">
<xsl:text>-----inside condition----------</xsl:text>
<xsl:value-of select="position()"/>
<xsl:element name="{$ns-prefix2}:LogException" namespace="{$ns-namespace2}">
<xsl:for-each select="//LogException[position()]/*">
<xsl:variable name="elementname">
<xsl:value-of select="name()"/>
</xsl:variable>
<xsl:element name="{$ns-prefix2}:{$elementname}" namespace="{$ns-namespace2}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
<xsl:call-template name="incrementValue">
<xsl:with-param name="value" select="$value + 1"/>
<xsl:with-param name="limit" select="$limit"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="/">
<xsl:element name="soapenv:Envelope" namespace="{$ns-namespace}">
<xsl:copy-of select="document('')/*/namespace::*[name()='soapenv' or name()='tem']"/>
<xsl:element name="{$ns-prefix}:Header" namespace="{$ns-namespace}"/>
<xsl:element name="{$ns-prefix}:Body" namespace="{$ns-namespace}">
<xsl:apply-templates select="//LogException"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I am getting result like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
-----Checking the value of the total no. of LogException node-------2--------Value checked-----------inside condition----------1
<tem:LogException>
<tem:exceptionNotificationGroup>Person</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Test</tem:exceptionType>
<tem:createdDatetime>2011-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>RMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
<tem:exceptionNotificationGroup>Vinayak</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Testing</tem:exceptionType>
<tem:createdDatetime>2012-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY Check</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>ORMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
</tem:LogException>
-----Checking the value of the total no. of LogException node-------2--------Value checked-----------inside condition----------2
<tem:LogException>
<tem:exceptionNotificationGroup>Person</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Test</tem:exceptionType>
<tem:createdDatetime>2011-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>RMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
<tem:exceptionNotificationGroup>Vinayak</tem:exceptionNotificationGroup>
<tem:exceptionType>Implimentation Testing</tem:exceptionType>
<tem:createdDatetime>2012-03-29</tem:createdDatetime>
<tem:exceptionSource>INFINITY Check</tem:exceptionSource>
<tem:exceptionSourceReferenceId>2345610</tem:exceptionSourceReferenceId>
<tem:exceptionTarget>ORMB</tem:exceptionTarget>
<tem:sourceSystem>ODS</tem:sourceSystem>
<tem:message>Mandatory Field Missing-Test</tem:message>
</tem:LogException>
</soapenv:Body>
</soapenv:Envelope>
does any one has anwser to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此 XSLT 1.0 样式表:
此 XSLT 2.0 样式表:
输出:
This XSLT 1.0 stylesheet:
This XSLT 2.0 stylesheet:
Output:
假设您只想深入两层,您可以尝试以下操作:
通常,XSLT 中的最佳实践是尽可能避免使用
标记。您可以定义更具体的模板,并使用
调用这些模板Assuming you only want to go down two levels deep, you can try this:
Usually, the best practice in XSLT is to avoid
<xsl:for-each>
tags wherever possible. You define ever more specific templates which you call with<xsl:apply-templates>