好吧,我已经看到了很多关于这个问题的变化,但没有一个完全回答我想要解决的问题,也许我只是太过密集而无法看到如何将其他答案应用于我正在尝试做的事情.
我有一些类似于以下内容的XML:
<?xml version="1.0" encoding="utf-8"?>
<message>
<cmd id="api_info">
<api-version>1.0</api-version>
<api-build>1.0.0.0</api-build>
</cmd>
</message>
Run Code Online (Sandbox Code Playgroud)
现在我有一个XSLT转换,我正在应用于这个XML.XSLT类似于以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
version="2.0">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="message"/>
</xsl:template>
<xsl:template match="message">
<xsl:element name="message" xmlns="http://www.companyname.com/schemas/product/Version001">
<xsl:apply-templates select="/message/cmd/@id"/>
</xsl:element>
</xsl:template>
<xsl:template match="/message/cmd/@id">
<xsl:variable name="_commandType" select="/message/cmd/@id"/>
<xsl:element name="messageHeader">
<xsl:element name="cmdType">
<xsl:value-of select="$_commandType"/>
</xsl:element>
</xsl:element>
<xsl:element name="messageBody">
<xsl:choose>
<xsl:when test="$_commandType = 'api_info'">
<xsl:element name="apiInfoBody">
<xsl:element name="apiVersion">
<xsl:value-of select="/message/cmd/api-version"/>
</xsl:element>
<xsl:element name="apiBuild">
<xsl:value-of select="/message/cmd/api-build"/>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:when test="$_commandType = 'communicationError'">
<xsl:element name="communicationErrorBody"> …Run Code Online (Sandbox Code Playgroud)