<?xml version="1.0" encoding="UTF-8"?>
<p:transformOutput xmlns:p="http://cfpe/export/objects">
<p:objectSet>
<p:objects>
<p:object>
<p:objectAttributes>
<p:objectType>a</p:objectType>
<p:attribute name="x">F600</p:attribute>
<p:attribute name="y">A100</p:attribute>
<p:attribute name="z">D400</p:attribute>
</p:objectAttributes>
</p:object>
<p:object>
<p:objectAttributes>
<p:objectType>b</p:objectType>
<p:attribute name="x">F600</p:attribute>
<p:attribute name="y">C300</p:attribute>
<p:attribute name="z">B200</p:attribute>
</p:objectAttributes>
</p:object>
<p:object>
<p:objectAttributes>
<p:objectType>b</p:objectType>
<p:attribute name="x">a</p:attribute>
<p:attribute name="y">A100</p:attribute>
<p:attribute name="z">B200</p:attribute>
</p:objectAttributes>
</p:object>
</p:objects>
</p:objectSet>
<ns1:Templates>
<ns1:Template>
<ns1:System_Class>a</ns1:System_Class>
<ns1:System_Table>A100</ns1:System_Table>
<ns1:System_Attribute>aA</ns1:System_Attribute>
</ns1:Template>
<ns1:Template>
<ns1:System_Class>b</ns1:System_Class>
<ns1:System_Table>B200</ns1:System_Table>
<ns1:System_Attribute>bB</ns1:System_Attribute>
</ns1:Template>
</ns1:Templates>
xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://cfpe/export/objects" xmlns:ns1="http://cfpe/export/objects"
exclude-result-prefixes="p">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/p:transformOutput">
<objects>
<xsl:for-each select="p:transformOutput/p:objectSet/p:objects/p:object">
<object type="{p:objectAttributes/p:objectType}">
<xsl:variable name="attributes" select="p:objectAttributes/*" />
<xsl:variable name="matching-template" select="p:transformOutput/ns1:Templates/ns1:Template[ns1:System_Class=$attributes and ns1:System_Table=$attributes]" />
<template>
<xsl:value-of select="tns:System_Attribute"/>
</template>
</object>
</xsl:for-each>
</objects>
</xsl:template>
</xsl:stylesheet>
匹配模板似乎找不到值路径。我试图比较System_Class(如果存在于objectAttributes中),并且如果objectAttributes中不存在System_Attribute的值,则从中获取System_Attribute
答案 0 :(得分:0)
首先匹配根元素p:transformOutput
,如下所示:
<xsl:template match="/p:transformOutput">
但是你做了xsl:for-each
,其中还包括根元素
<xsl:for-each select="p:transformOutput/p:objectSet/p:objects/p:object">
您已经定位在p:transformOutput
上,因此这将寻找一个不存在的子元素。你需要把它改成这个;
<xsl:for-each select="p:objectSet/p:objects/p:object">
您稍后会在matching-template
变量
<xsl:variable name="matching-template"
select="p:transformOutput/ns1:Templates/ns1:Template[ns1:System_Class=$attributes and ns1:System_Table=$attributes]" />
但此时您处于xsl:for-each
构造中,位于p:object
上。您需要使xpath表达式成为绝对表达式,以允许您再次从根目录进行搜索。
<xsl:variable name="matching-template"
select="/p:transformOutput/ns1:Templates/ns1:Template[ns1:System_Class=$attributes and ns1:System_Table=$attributes]"/>
最后,你试着像这样System_Attribute
:
<xsl:value-of select="tns:System_Attribute"/>
但此时你仍然位于p:object
。您需要在表达式中包含matching-template
变量:
<xsl:value-of select="$matching-template/ns1:System_Attribute"/>
试试这个XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:p="http://cfpe/export/objects" xmlns:ns1="http://cfpe/export/objects" exclude-result-prefixes="p">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/p:transformOutput">
<objects>
<xsl:for-each select="p:objectSet/p:objects/p:object">
<object type="{p:objectAttributes/p:objectType}">
<xsl:variable name="attributes" select="p:objectAttributes/*"/>
<xsl:variable name="matching-template" select="/p:transformOutput/ns1:Templates/ns1:Template[ns1:System_Class=$attributes and ns1:System_Table=$attributes]"/>
<template>
<xsl:value-of select="$matching-template/ns1:System_Attribute"/>
</template>
</object>
</xsl:for-each>
</objects>
</xsl:template>
</xsl:stylesheet>