Xml 如何嵌套XSL选择以避免';不必使用相同的XPath
我有下面的XSL,它可以根据需要工作。然而,一遍又一遍地重复使用相同的Xml 如何嵌套XSL选择以避免';不必使用相同的XPath,xml,xslt,xpath,transform,Xml,Xslt,Xpath,Transform,我有下面的XSL,它可以根据需要工作。然而,一遍又一遍地重复使用相同的客户/人员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="/
客户/人员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>
对不起,如果以前有人问过这个问题。我试着四处寻找答案,但我觉得我可能只是在寻找错误的东西
提前感谢您的建议。您可以使用变量来存储多次使用的节点:
<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>
此外,您还可以通过使用以下命令来减少详细信息:
继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>
这也可以通过模板规则完成:
<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”
。这里对XSLT版本有一些混淆。样式表显示version=“1.0”,但它使用的是需要XSLT2.0的xsl:attribute/@select。
<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>
...