使用XSLT在结果xml的两个不同元素中使用xml属性值
我有一个如下的源XML使用XSLT在结果xml的两个不同元素中使用xml属性值,xml,xslt,Xml,Xslt,我有一个如下的源XML <parent> <child id="123456">Child Name </child> <image name="child.jpg"> </parent> 子名称 目标XML应该是 <data> <person id="123456"> <name>Child Name</name>
<parent>
<child id="123456">Child Name
</child>
<image name="child.jpg">
</parent>
子名称
目标XML应该是
<data>
<person id="123456">
<name>Child Name</name>
</person>
<relation id="123456">
<filename>child.jpg</filename>
</relation>
</data>
子名称
child.jpg
我正在使用XSLT对其进行转换问题是,何时可以使用XSLT从目标XML中两个不同位置的源XML中获取id值(即123456) 您可以尝试以下方法:
XSL:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<data>
<xsl:for-each select="parent">
<!--variable to store @id-->
<xsl:variable name="id" select="child/@id"/>
<!--creating a test comment node()-->
<xsl:comment>Child having id: <xsl:value-of select="child/@id"/></xsl:comment>
<xsl:element name="person">
<xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
<xsl:element name="name">
<xsl:value-of select="./child/text()"/>
</xsl:element>
</xsl:element>
<xsl:element name="relation">
<xsl:attribute name="id">
<xsl:value-of select="$id"/>
</xsl:attribute>
<xsl:element name="filename">
<xsl:value-of select="./image/@name"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>
<data>
<person id="123456">
<name>Child Name</name>
</person>
<relation id="123456">
<filename>child.jpg</filename>
</relation>
</data>
有身份证的儿童:
输入XML(您的,但稍作修改以使其格式良好)
子名称
和结果
<?xml version='1.0' ?>
<data>
<!--Child having id: 123456-->
<person id="123456">
<name>Child Name</name>
</person>
<relation id="123456">
<filename>child.jpg</filename>
</relation>
</data>
子名称
child.jpg
这里有一个简短而简单的解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="parent">
<data>
<xsl:apply-templates/>
</data>
</xsl:template>
<xsl:template match="child">
<person id="{@id}">
<name><xsl:value-of select="."/></name>
</person>
</xsl:template>
<xsl:template match="image">
<relation id="{preceding-sibling::child[1]/@id}">
<filename><xsl:value-of select="@name"/></filename>
</relation>
</xsl:template>
</xsl:stylesheet>
<parent>
<child id="123456">Child Name</child>
<image name="child.jpg"/>
</parent>
当此转换应用于提供的XML文档时:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="parent">
<data>
<xsl:apply-templates/>
</data>
</xsl:template>
<xsl:template match="child">
<person id="{@id}">
<name><xsl:value-of select="."/></name>
</person>
</xsl:template>
<xsl:template match="image">
<relation id="{preceding-sibling::child[1]/@id}">
<filename><xsl:value-of select="@name"/></filename>
</relation>
</xsl:template>
</xsl:stylesheet>
<parent>
<child id="123456">Child Name</child>
<image name="child.jpg"/>
</parent>
子名称
生成所需的正确结果:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<data>
<xsl:for-each select="parent">
<!--variable to store @id-->
<xsl:variable name="id" select="child/@id"/>
<!--creating a test comment node()-->
<xsl:comment>Child having id: <xsl:value-of select="child/@id"/></xsl:comment>
<xsl:element name="person">
<xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
<xsl:element name="name">
<xsl:value-of select="./child/text()"/>
</xsl:element>
</xsl:element>
<xsl:element name="relation">
<xsl:attribute name="id">
<xsl:value-of select="$id"/>
</xsl:attribute>
<xsl:element name="filename">
<xsl:value-of select="./image/@name"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>
<data>
<person id="123456">
<name>Child Name</name>
</person>
<relation id="123456">
<filename>child.jpg</filename>
</relation>
</data>
子名称
child.jpg
@dradu:在解决这个问题时不需要使用变量——请看我的答案。@tuseef:您引用的“简单”是不必要的复杂(没有xsl:element
和xsl:attribute'是必需的)、低效且可能存在错误(
祖先::MainStory/@kmsid`扫描所有祖先并通常选择多个节点)helping@Touseef:我很高兴我的答案对你有用。请你接受这个答案(点击答案旁边的复选标记)?小心-虽然这个答案适用于OP的问题,但它可能没有@Dimitre Novatchev提出的答案那么全面。例如,如果它们是
元素中的两个/
元素对,您的答案只会返回第一个。同样,这不是OP明确要求的场景,但它是值得的看着。