我有下面的XSL,它可以根据需要运行。但是,不得不一遍又一遍地重用相同的Customer/Personnel
XPath而感到麻烦。有没有办法可以嵌套XSL,以便嵌套节点中的任何内容都使用相同的前缀XPath。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<root>
<ContactInternal>
<xsl:attribute name="UserShortName" select="Customer/Personnel/Party/ShortName" />
<xsl:attribute name="UserName" select="Customer/Personnel/Party/Name" />
<xsl:attribute name="Email" select="Customer/Personnel/Party/ContactAddresses/ContactAddress/EmailAddress/EMail" />
<xsl:if test="Customer/Personnel/Party/ContactAddresses/ContactAddress/Telephone/Number">
<xsl:attribute name="Telephone" select="Customer/Personnel/Party/ContactAddresses/ContactAddress/Telephone/Number" />
</xsl:if>
<xsl:if test="Customer/Personnel/JobTitle">
<xsl:attribute name="JobTitle" select="Customer/Personnel/JobTitle" />
</xsl:if>
<xsl:if test="Customer/Personnel/Branch/Code">
<xsl:attribute name="BranchCode" select="Customer/Personnel/Branch/Code" />
</xsl:if>
</ContactInternal>
</root>
</xsl:template>
</xsl:stylesheet>
很抱歉,如果之前有人询问过。我试着去寻找答案,但我觉得我可能只是在寻找错误的东西。
提前感谢任何建议。
答案 0 :(得分:0)
您可以使用变量来存储您多次使用的节点:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:variable name="personnel"
select="Customer/Personnel" />
<xsl:variable name="party"
select="$personnel/Party" />
<xsl:variable name="contactAddress"
select="$party/ContactAddresses/ContactAddress" />
<root>
<ContactInternal>
<xsl:attribute name="UserShortName" select="$party/ShortName" />
<xsl:attribute name="UserName" select="$party/Name" />
<xsl:attribute name="Email" select="$contactAddress/EmailAddress/EMail" />
<xsl:if test="$contactAddress/Telephone/Number">
<xsl:attribute name="Telephone" select="$contactAddress/Telephone/Number" />
</xsl:if>
<xsl:if test="$personnel/JobTitle">
<xsl:attribute name="JobTitle" select="$personnel/JobTitle" />
</xsl:if>
<xsl:if test="$personnel/Branch/Code">
<xsl:attribute name="BranchCode" select="Customer/Personnel/Branch/Code" />
</xsl:if>
</ContactInternal>
</root>
</xsl:template>
此外,您可以使用attribute value templates:
使其更简洁<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:variable name="personnel"
select="Customer/Personnel" />
<xsl:variable name="party"
select="$personnel/Party" />
<xsl:variable name="contactAddress"
select="$party/ContactAddresses/ContactAddress" />
<root>
<ContactInternal UserShortName="{ $party/ShortName }"
UserName="{ $party/Name }"
Email="{ $contactAddress/EmailAddress/EMail }">
<xsl:if test="$contactAddress/Telephone/Number">
<xsl:attribute name="Telephone" select="$contactAddress/Telephone/Number" />
</xsl:if>
<xsl:if test="$personnel/JobTitle">
<xsl:attribute name="JobTitle" select="$personnel/JobTitle" />
</xsl:if>
<xsl:if test="$personnel/Branch/Code">
<xsl:attribute name="BranchCode" select="Customer/Personnel/Branch/Code" />
</xsl:if>
</ContactInternal>
</root>
</xsl:template>
答案 1 :(得分:0)
继JLRishe的回答之后,在XSLT 3.0中你可以让它更紧凑:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:template match="/">
<xsl:variable name="personnel"
select="Customer/Personnel" />
<xsl:variable name="party"
select="$personnel/Party" />
<xsl:variable name="contactAddress"
select="$party/ContactAddresses/ContactAddress" />
<root>
<ContactInternal UserShortName="{ $party/ShortName }"
UserName="{ $party/Name }"
Email="{ $contactAddress/EmailAddress/EMail }">
<xsl:where-populated>
<xsl:attribute name="Telephone"
select="$contactAddress/Telephone/Number" />
<xsl:attribute name="JobTitle"
select="$personnel/JobTitle" />
<xsl:attribute name="BranchCode"
select="$personnel/Branch/Code" />
</xsl:where-populated>
</ContactInternal>
</root>
</xsl:template>
答案 2 :(得分:0)
这也可以通过模板规则来完成:
<xsl:template match="/">
<root>
<ContactInternal>
<xsl:apply-templates/>
</ContactInternal>
</root>
</xsl:template>
<xsl:template match="ShortName">
<xsl:attribute name="UserShortName" select="."/>
</xsl:template>
<xsl:template match="Name">
<xsl:attribute name="UserName" select="."/>
</xsl:template>
<xsl:template match="EMail">
<xsl:attribute name="Email" select="."/>
</xsl:template>
...
(如果需要消除歧义,则将模式限定为match="Party/Name"
。)