当前位置: 代码迷 >> XML/SOAP >> xslt 解析,该如何处理
  详细解决方案

xslt 解析,该如何处理

热度:766   发布时间:2014-02-23 23:10:44.0
xslt 解析
xml文件

<?xml version="1.0" encoding="UTF-8"?>
<Data>
<NaturalPerson>
<Title>MR</Title>
<Gender>M</Gender>
</NaturalPerson>
<NaturalPerson>
<Title>MI</Title>
<Gender>F</Gender>
</NaturalPerson>
</Data>

需要解析生成

<?xml version="1.0" encoding="UTF-8"?>
<TR04_NATURAL_PERSON>
<cstd_gender>M</cstd_gender>
<cstd_title>MR</cstd_title>
</TR04_NATURAL_PERSON>
<TR04_NATURAL_PERSON>
<cstd_gender>F</cstd_gender>
<cstd_title>MI</cstd_title>
</TR04_NATURAL_PERSON>

其中,NaturalPerson---》解析成TR04_NATURAL_PERSON,Title --》解析成cstd_title,Gender---》cstd_gender,另外,还要求,解析后,cstd_gender 排在cstd_title上面。
我写的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="//NaturalPerson">
    <!--<xsl:for-each select="//NaturalPerson">-->
<xsl:for-each select="child::*">
<TR04_NATURAL_PERSON>
 <xsl:if test="local-name()='Title'">
<xsl:variable name="eleName" select="//Gender"/>
<cstd_gender><xsl:value-of select="$eleName/text()"/></cstd_gender>
</xsl:if>
<xsl:if test="local-name()='Gender'">
<xsl:variable name="eleName" select="//Title"/>
<cstd_title><xsl:value-of select="$eleName/text()"/></cstd_title>
</xsl:if>
</TR04_NATURAL_PERSON> 
</xsl:for-each>
    <!--</xsl:for-each> -->

</xsl:template>

</xsl:stylesheet>

出来的结果:

<?xml version="1.0" encoding="UTF-8"?>
<TR04_NATURAL_PERSON>
<cstd_gender>M</cstd_gender>
</TR04_NATURAL_PERSON>
<TR04_NATURAL_PERSON>
<cstd_title>MR</cstd_title>
</TR04_NATURAL_PERSON>
<TR04_NATURAL_PERSON>
<cstd_gender>M</cstd_gender>
</TR04_NATURAL_PERSON>
<TR04_NATURAL_PERSON>
<cstd_title>MR</cstd_title>
</TR04_NATURAL_PERSON>

疑问:
TR04_NATURAL_PERSON下的cstd_gender和cstd_title为何分撒出现在TR04_NATURAL_PERSON标签下?<xsl:for-each>循环嵌套,怎么控制select?请达人给予帮助,谢谢!
<?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="/">     
<xsl:for-each select="Data/NaturalPerson">
<TR04_NATURAL_PERSON>


<cstd_gender><xsl:value-of select="Gender"/></cstd_gender>


<cstd_title><xsl:value-of select="Title"/></cstd_title>
                            
</TR04_NATURAL_PERSON>
</xsl:for-each>
    

    </xsl:template>
    
</xsl:stylesheet>

试试