XSLT不会在XML节点中创建标记
我必须复制来自源xml的所有标记,并使用源数据的元素(HostedAppcode)创建其他xml标记。我创建了XSLT,但没有得到所需的输出。请看下面 非常感谢您的帮助 XML(输入)XSLT不会在XML节点中创建标记,xml,xslt,Xml,Xslt,我必须复制来自源xml的所有标记,并使用源数据的元素(HostedAppcode)创建其他xml标记。我创建了XSLT,但没有得到所需的输出。请看下面 非常感谢您的帮助 XML(输入) SE112935 VLY0 UBD0、YND0、UGZ0、WWS0 SE112936 YND0、UGZ0、WWS0 YND0、UGZ0、WWS0 我创建的XSLT: <?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl
SE112935
VLY0
UBD0、YND0、UGZ0、WWS0
SE112936
YND0、UGZ0、WWS0
YND0、UGZ0、WWS0
我创建的XSLT:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<xsl:for-each select="ServerDetails">
<ServerDetails>
<HostedApplications>
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="HostedAppcode"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</HostedApplications>
</ServerDetails>
</xsl:for-each>
</xsl:template>
<xsl:template name="tokenizeString">
<!--passed template parameter -->
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list, $delimiter)">
<item>
<!-- get everything in front of the first delimiter -->
<xsl:value-of select="substring- before($list,$delimiter)"/>
</item>
<xsl:call-template name="tokenizeString">
<!-- store anything left in another variable -->
<xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<item>
<xsl:value-of select="$list"/>
</item> `enter code here`
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
`在这里输入代码`
我得到的输出
<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfServerDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ServerDetails>
<ServerName>SE112935</ServerName>
<InfrastructureAppcode>VLY0</InfrastructureAppcode>
<HostedAppcode>UBD0,YND0,UGZ0,WWS0</HostedAppcode>
</ServerDetails>
<ServerDetails>
<ServerName>SE112936</ServerName>
<HostedAppcode>YND0,UGZ0,WWS0</HostedAppcode>
<FinancialAppCode>YND0,UGZ0,WWS0</FinancialAppCode>
</ServerDetails>
</ArrayOfServerDetails>
<ServerDetails>
<HostedApplications>
<item>UBD0</item>
<item>YND0</item>
<item>UGZ0</item>
<item>WWS0</item>
</HostedApplications>
</ServerDetails>
<ServerDetails>
<HostedApplications>
<item>YND0</item>
<item>UGZ0</item>
<item>WWS0</item>
</HostedApplications>
</ServerDetails>
SE112935
VLY0
UBD0、YND0、UGZ0、WWS0
SE112936
YND0、UGZ0、WWS0
YND0、UGZ0、WWS0
UBD0
YND0
UGZ0
WWS0
YND0
UGZ0
WWS0
期望(输出)
SE112935
VLY0
UBD0、YND0、UGZ0、WWS0
UBD0
YND0
UGZ0
WWS0
SE112936
YND0、UGZ0、WWS0
YND0
UGZ0
WWS0
YND0、UGZ0、WWS0
对于模板中的ServerDetails
元素(匹配任何节点),不要为每个xsl:for每个,而是将其作为单独的匹配模板
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ServerDetails">
<ServerDetails>
<xsl:apply-templates select="@*|node()"/>
<HostedApplications>
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="HostedAppcode"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</HostedApplications>
</ServerDetails>
</xsl:template>
<xsl:template name="tokenizeString">
<!--passed template parameter -->
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list, $delimiter)">
<item>
<!-- get everything in front of the first delimiter -->
<xsl:value-of select="substring-before($list,$delimiter)"/>
</item>
<xsl:call-template name="tokenizeString">
<!-- store anything left in another variable -->
<xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<item>
<xsl:value-of select="$list"/>
</item>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
这是因为您两次“解析”了
元素。下面的代码匹配所有元素并递归地复制它们的内容
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
这就是生成第一组输出,它反映了现有的XML内容。然后使用for each遍历所有
元素,并输出指定的内容
这是下面未测试的代码,但基本上它应该复制除HostedAppCode元素之外的所有元素。对于该元素,它将首先复制它,然后输出所需的附加
元素
<xsl:template match="HostedAppcode">
<xsl:copy></xsl:copy>
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="text()"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
让我试试……我猜你是在建议我做a)添加显示所有内容的代码b)删除服务器详细信息标记c)添加提供的模板d)保留其余代码我尝试了以下操作……它不起作用:(续…)我认为您首先复制节点,然后创建所需的标记。因此,请将match=“@*|node()”限制为不复制您的
ServerDetails`
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<xsl:template match="HostedAppcode">
<xsl:copy></xsl:copy>
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="text()"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>