我坚持这个问题。
我有这个 XML:
<Document>
<Type>ABC</Type>
<Header>
<Date>15-01-2017</Date>
<Time>11:00 AM</Time>
</Header>
<Body>
<Name>Juan</Name>
<Age>10</Age>
<Address>
<City>City</City>
<Country>Country</Country>
<Block>Block</Block>
</Address>
</Body>
</Document>
我有这个 XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:variable name ="DocumentType" select ="//Document/Type" />
<xsl:template match ="Document">
<Root>
<xsl:apply-templates select ="Header" />
<xsl:element name="Data{$DocumentType}">
<xsl:call-template name="Data">
<xsl:with-param name="type" select ="$DocumentType" />
</xsl:call-template>
</xsl:element>
</Root>
</xsl:template>
<xsl:template name ="Data">
<xsl:param name="type" />
<Name>
<xsl:value-of select ="Body/Name" />
</Name>
<Age>
<xsl:value-of select ="Body/Age" />
</Age>
<xsl:element name="Address{$type}">
<City>
<xsl:value-of select ="City"/>
</City>
<Country>
<xsl:value-of select ="Country"/>
</Country>
<Block>
<xsl:value-of select ="Block"/>
</Block>
</xsl:element>
</xsl:template>
<xsl:template match ="Header">
<Header>
<DateCreated>
<xsl:value-of select ="Date"/>
</DateCreated>
<TimeCreated>
<xsl:value-of select ="Time"/>
</TimeCreated>
</Header>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
我的问题是我无法访问 XML 中地址的子节点的值,我只看到了这个:
<Root>
<Header>
<DateCreated>15-01-2017</DateCreated>
<TimeCreated>11:00 AM</TimeCreated>
</Header>
<DataABC>
<Name>Juan</Name>
<Age>10</Age>
<AddressABC>
<City></City>
<Country></Country>
<Block></Block>
</AddressABC>
</DataABC>
</Root>
谁能帮我告诉我我的错误。
谢谢
您的错误已在迈克尔凯的回答中指出。
我想建议一种替代方法 - 一种需要更少工作的方法:
XSLT 1.0
<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:strip-space elements="*"/>
<xsl:variable name="DocumentType" select="/Document/Type" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Document">
<Root>
<xsl:apply-templates select="Header | Body"/>
</Root>
</xsl:template>
<xsl:template match="Body">
<xsl:element name="Data{$DocumentType}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Date | Time">
<xsl:element name="{name()}Created">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Address">
<xsl:element name="Address{$DocumentType}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
请注意,全局变量可以从任何模板访问,不需要作为参数发送。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句