访问xsl中的节点
问题描述:
我刚开始使用xsl。下面将描述我想要完成的事情,并希望有人能够提供帮助。访问xsl中的节点
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:GetSkillResponse>
<ns1:Skill>
<ns1:Person>
<ns1:SkillId>001</ns1:SkillId>
</ns1:Person>
<ns1:Task Active="N">Value 1</ns1:Task>
<ns1:Task Active="Y">Value 2</ns1:Task>
<ns1:Task Active="Y">Value 3</ns1:Task>
</ns1:Skill>
<ns1:Skill>
<ns1:Person>
<ns1:SkillId>002</ns1:SkillId>
</ns1:Person>
<ns1:Task Active="Y">Value 1</ns1:Task>
<ns1:Task Active="Y">Value 2</ns1:Task>
<ns1:Task Active="Y">Value 3</ns1:Task>
</ns1:Skill>
</ns1:GetSkillResponse>
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ns1="http://test">
<xsl:template name="GetSkillResponse">
<Message>
<xsl:for-each select="/ns1:GetSkillResponse/ns1:Skill">
<PropertySet>
<xsl:attribute name="MySkillId"><xsl:value-of select="ns1:Person/ns1:SkillId"/></xsl:attribute>
<xsl:attribute name="MyTask"><xsl:value-of select="ns1:Task"/></xsl:attribute>
<xsl:attribute name="MyActiveFlag"><xsl:value-of select="ns1:Task/@Active"/></xsl:attribute>
</PropertySet>
</xsl:for-each>
</Message>
</xsl:template>
</xsl:stylesheet>
这让我:
001,值1,N
002,值1,Y
但我需要得到:
001,值1,N
001,值2,Y
001,值3,Y
002,值1,Y
002,值2,Y
00 2,Value 3,Y
这可能吗?
答
它看起来像你想要一个单独的条目Skill
下的每个Task
元素,要做到这一点,只需要嵌套xsl:for-each
。
例如...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://test">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:call-template name="GetSkillResponse" />
</xsl:template>
<xsl:template name="GetSkillResponse">
<Message>
<xsl:for-each select="/ns1:GetSkillResponse/ns1:Skill">
<xsl:for-each select="ns1:Task">
<PropertySet>
<xsl:attribute name="MySkillId"><xsl:value-of select="../ns1:Person/ns1:SkillId"/></xsl:attribute>
<xsl:attribute name="MyTask"><xsl:value-of select="."/></xsl:attribute>
<xsl:attribute name="MyActiveFlag"><xsl:value-of select="@Active"/></xsl:attribute>
</PropertySet>
</xsl:for-each>
</xsl:for-each>
</Message>
</xsl:template>
</xsl:stylesheet>
注意你可以用Attribute Value Templates
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://test">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:call-template name="GetSkillResponse" />
</xsl:template>
<xsl:template name="GetSkillResponse">
<Message>
<xsl:for-each select="/ns1:GetSkillResponse/ns1:Skill">
<xsl:for-each select="ns1:Task">
<PropertySet MySkillId="{../ns1:Person/ns1:SkillId}" MyTask="{.}" MyActiveFlag="{@Active}"/>
</xsl:for-each>
</xsl:for-each>
</Message>
</xsl:template>
</xsl:stylesheet>
非常感谢简化这个,这是我需要什么。 – Paul