Xml 如何选择属性的下一个同级
我想选择一个属性的下一个同级来输出句点。 此模板正在选择所需的属性 唯一有效的解决方案是匹配属性名称并选择所需的名称。我怎样才能使它更通用,使用很少的行? 我尝试过“跟随兄弟姐妹”,但这只适用于元素 功能代码片段Xml 如何选择属性的下一个同级,xml,xslt,xpath,Xml,Xslt,Xpath,我想选择一个属性的下一个同级来输出句点。 此模板正在选择所需的属性 唯一有效的解决方案是匹配属性名称并选择所需的名称。我怎样才能使它更通用,使用很少的行? 我尝试过“跟随兄弟姐妹”,但这只适用于元素 功能代码片段 ... <Period > <xsl:attribute name="Unit"> <xsl:choose&g
...
<Period >
<xsl:attribute name="Unit">
<xsl:choose>
<xsl:when test="local-name()='BonusAmount'">
<xsl:value-of select="../@BonusFrequency"/>
</xsl:when>
<xsl:when test="local-name()='CommissionAmount'">
<xsl:value-of select="../@CommissionFrequency"/>
</xsl:when>
<xsl:when test="local-name()='GrossRegularOvertimeAmount'">
<xsl:value-of select="../@GrossRegularOvertimeFrequency"/>
</xsl:when>
<xsl:when test="local-name()='GrossSalaryAmount'">
<xsl:value-of select="../@GrossSalaryFrequency"/>
</xsl:when>
<xsl:when test="local-name()='CarAllowanceAmount'">
<xsl:value-of select="../@CarAllowanceFrequency"/>
</xsl:when>
<xsl:when test="local-name()='WorkAllowanceAmount'">
<xsl:value-of select="../@WorkAllowanceFrequency"/>
</xsl:when>
<xsl:when test="local-name()='WorkersCompensationAmount'">
<xsl:value-of select="../@WorkersCompensationFrequency"/>
</xsl:when>
</xsl:choose>
</xsl:attribute>
</Period>
...
。。。
...
XML示例:
<Employment>
<PAYG Basis="Temporary" Industry="Oil and Gas Extraction" IndustryCode="0700" Occupation="General Waiter" OccupationCode="6323-11" OnProbation="Yes" ProbationDateEnds="2019-03-27" StartDate="2014-05-05" Status="Secondary" UniqueID="c8492d8c-34fc-419b-93f4-f1f3" x_Employer="c46c9077-31ef-4daa-b8cc-c9e3">
<Income BonusAmount="89898985" BonusFrequency="Monthly" CommissionAmount="4488" CommissionFrequency="Yearly" GrossRegularOvertimeAmount="365" GrossRegularOvertimeFrequency="Fortnightly" GrossSalaryAmount="4798" GrossSalaryFrequency="Weekly" WorkAllowanceAmount="10101010" WorkAllowanceFrequency="Monthly"/>
</PAYG>
</Employment>
输出:
<ValueItem Value="89898985">
<Identifier UniqueID="c8492d8c-34fc-419b-93f4-f1f3-Income-PAYG-BonusAmount"/>
<PercentOwned Percent="100">
<RelatedEntityRef RelatedID="baaef85e-3793-4fe8-8c62-8cc766fa490b"/>
</PercentOwned>
<Income Type="Bonus">
<Period Unit="Monthly"/>
<RelatedEntityRef RelatedID="c46c9077-31ef-4daa-b8cc-c9e3"/>
</Income>
</ValueItem>
<ValueItem Value="4488">
<Identifier UniqueID="c8492d8c-34fc-419b-93f4-f1f3-Income-PAYG-CommissionAmount"/>
<PercentOwned Percent="100">
<RelatedEntityRef RelatedID="baaef85e-3793-4fe8-8c62-8cc766fa490b"/>
</PercentOwned>
<Income Type="Commission">
<Period Unit="Yearly"/>
<RelatedEntityRef RelatedID="c46c9077-31ef-4daa-b8cc-c9e3"/>
</Income>
</ValueItem>
<ValueItem Value="365">
<Identifier UniqueID="c8492d8c-34fc-419b-93f4-f1f3-Income-PAYG-GrossRegularOvertimeAmount"/>
<PercentOwned Percent="100">
<RelatedEntityRef RelatedID="baaef85e-3793-4fe8-8c62-8cc766fa490b"/>
</PercentOwned>
<Income Type="GrossRegularOvertime">
<Period Unit="Fortnightly"/>
<RelatedEntityRef RelatedID="c46c9077-31ef-4daa-b8cc-c9e3"/>
</Income>
</ValueItem>
<ValueItem Value="4798">
<Identifier UniqueID="c8492d8c-34fc-419b-93f4-f1f3-Income-PAYG-GrossSalaryAmount"/>
<PercentOwned Percent="100">
<RelatedEntityRef RelatedID="baaef85e-3793-4fe8-8c62-8cc766fa490b"/>
</PercentOwned>
<Income Type="GrossSalary">
<Period Unit="Weekly"/>
<RelatedEntityRef RelatedID="c46c9077-31ef-4daa-b8cc-c9e3"/>
</Income>
</ValueItem>
等等。对于XSLT 2.0及更高版本,您可以使用,如下所示:
<Period Unit="{../@*[name()= replace(name(current()),'Amount','Frequency')]}"/>
<xsl:template match="PAYG">
<test>
<xsl:apply-templates select="Income/@*" />
</test>
</xsl:template>
<xsl:template match="Income/@*">
<xsl:variable name="a" select="local-name()" />
<xsl:if test="'Amount' = substring( $a, string-length($a) - string-length('Amount') + 1 )">
<xsl:variable name="f" select="concat( substring-before( $a, 'Amount' ), 'Frequency' )"/>
<Period >
<xsl:attribute name="Unit">
<xsl:value-of select="../@*[local-name() = $f]"/>
</xsl:attribute>
<xsl:value-of select="local-name()"/>
</Period>
</xsl:if>
</xsl:template>
注:
可用于访问当前XSLT文档document(“”)
- 自定义名称空间允许将数据嵌入样式表
可防止名称空间泄漏到输出文档中排除结果前缀
- 可以使用属性值模板(大括号)代替
(只要属性名称是固定的,就像您的情况一样) - 请注意模板模式,以将其与可能匹配的其他模板区分开来。需要在应用模板时进行设置
另一种可能是包含或使用—许多XSLT1.0处理器都支持EXSLT。我不确定我是否完全理解这一点。
Buf如果您需要查找以Amoun结尾的每个属性,请查找以频率结尾的属性 您可以尝试这样的破解:
<Period Unit="{../@*[name()= replace(name(current()),'Amount','Frequency')]}"/>
<xsl:template match="PAYG">
<test>
<xsl:apply-templates select="Income/@*" />
</test>
</xsl:template>
<xsl:template match="Income/@*">
<xsl:variable name="a" select="local-name()" />
<xsl:if test="'Amount' = substring( $a, string-length($a) - string-length('Amount') + 1 )">
<xsl:variable name="f" select="concat( substring-before( $a, 'Amount' ), 'Frequency' )"/>
<Period >
<xsl:attribute name="Unit">
<xsl:value-of select="../@*[local-name() = $f]"/>
</xsl:attribute>
<xsl:value-of select="local-name()"/>
</Period>
</xsl:if>
</xsl:template>
这将输出:
<test>
<Period Unit="Monthly">BonusAmount</Period>
<Period Unit="Yearly">CommissionAmount</Period>
<Period Unit="Fortnightly">GrossRegularOvertimeAmount</Period>
<Period Unit="Weekly">GrossSalaryAmount</Period>
<Period Unit="Monthly">WorkAllowanceAmount</Period>
</test>
博努萨蒙特
佣金金额
GrossRegularVertimeamount
格罗萨拉亚蒙
工作负载
如果没有示例XML,就不清楚您所说的起始属性和下一个同级的实际位置……您能显示一个XML示例吗?请注意,“兄弟”的概念不适用于属性,因为XML DOM不关心属性顺序。然而,在您的特定情况下,看起来您正在寻找以“频率”结尾的属性,而不是以“数量”结尾的属性。如果是这样,可能会使XSLT更通用,但有些字符串处理函数。@har07后面的数量需要是一个频率。这不会将字符串和来自BonusFrequency、CommissionFrequencc等的值添加到属性单位中?哦,当然。您的示例XML让它更清晰了,请查看更新的答案。如果XSLT处理器实现XSLT 3.0,您就不必为我的答案所建议的XSLT 1.0特定方法而烦恼了。只需使用fn:replace(name(current()),'Amount','Frequency')
。谢谢你,托马拉克,这种方法更好更简单。正是我想要的。为此,我使用了variable和substring(),但您的方法要好得多。最后的代码是:完全正确。花点时间,仔细阅读可用的XPath函数列表。如果您不知道replace(),那么很可能您也错过了很多其他内容。谢谢hr_117。经过一些测试,我实现了一些类似的东西,但Tomalak解决方案甚至更好。