我对XPath和XSLT很陌生。我正在编写一个XSLT,每当它在输入中发现“complexType”作为类型属性时,它就会递归地应用模板。
请注意,识别复杂类型的逻辑很好,所以我不介意。
下面是我的输入:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Object1">
<xs:all>
<xs:element minOccurs="0" name="segment-1" type="xs:string" />
<xs:element minOccurs="0" name="segment-2" type="xs:string" />
<xs:element minOccurs="0" name="complexType1" type="anyComplexType" />
</xs:all>
</xs:complexType>
<xs:complexType name="anyComplexType">
<xs:all>
<xs:element minOccurs="0" name="complexType2" type="anotherComplexType" />
</xs:all>
</xs:complexType>
<xs:complexType name="anyComplexType">
<xs:all>
<some Element>
</xs:all>
</xs:complexType>
</xs:schema>这是XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.approuter.com/schemas/cdk/api/">
<xsl:output indent="yes" xml:space="preserve" method="xml" />
<xsl:variable name="ObjectType" select=" 'Object1' " />
<xsl:template match="/">
<xsl:element name="object">
<xsl:attribute name="name" select="$ObjectType" />
<xsl:attribute name="label" select="$ObjectType" />
<xsl:attribute name="minCount">1</xsl:attribute>
<xsl:attribute name="maxCount">1</xsl:attribute>
<xsl:apply-templates select="//*:complexType[@name = $ObjectType]" />
</xsl:element>
</xsl:template>
<xsl:template match="*:complexType">
<!-- Logic to fing complexType' goes here -->
<xsl:call-template name="Elements" />
</xsl:template>
<xsl:template name="Elements">
<xsl:apply-templates select="//*:complexType[@name =@type]" />
</xsl:template>
</xsl:stylesheet>解释以下流程:
第四步不是为我工作。我相信这其中存在一些XPath模式或路径问题。
发布于 2015-01-09 08:44:02
您的主要问题出现在这一行中:
<xsl:apply-templates select="//*:complexType[@name = @type]" />您正在寻找任何xs:complexType,其name属性与其自己的@type属性匹配。但是,没有任何xs:complexType元素具有type属性。
具有xs:element属性的是type,但是当您执行xsl:apply-templates时,您将定位在xs:complexType元素上。调用命名模板不会改变上下文。
要解决这个问题,您可以更改命名的模板Elements,以包含选择element元素的代码。您还需要使用current()函数来引用当前上下文( element而不是您要选择的complexType )
<xsl:template name="Elements">
<xsl:for-each select="*/*:element">
<xsl:apply-templates select="//*:complexType[@name = current()/@type]" />
</xsl:for-each>
</xsl:template>不过,最好取消命名模板,而使用模板匹配。试试这个XSLT
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.approuter.com/schemas/cdk/api/">
<xsl:output indent="yes" xml:space="preserve" method="xml" />
<xsl:variable name="ObjectType" select=" 'Object1' " />
<xsl:template match="/">
<xsl:element name="object">
<xsl:attribute name="name" select="$ObjectType" />
<xsl:attribute name="label" select="$ObjectType" />
<xsl:attribute name="minCount">1</xsl:attribute>
<xsl:attribute name="maxCount">1</xsl:attribute>
<xsl:apply-templates select="//*:complexType[@name = $ObjectType]" />
</xsl:element>
</xsl:template>
<xsl:template match="*:complexType">
<xsl:value-of select="@name" />
<xsl:apply-templates select="*/*:element" />
</xsl:template>
<xsl:template match="*:element">
<xsl:apply-templates select="//*:complexType[@name = current()/@type]" />
</xsl:template>
</xsl:stylesheet>注意,实际上最好使用xsl:key来查找complexType记录。也试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.approuter.com/schemas/cdk/api/">
<xsl:output indent="yes" xml:space="preserve" method="xml" />
<xsl:key name="complexType" match="*:complexType" use="@name" />
<xsl:variable name="ObjectType" select=" 'Object1' " />
<xsl:template match="/">
<xsl:element name="object">
<xsl:attribute name="name" select="$ObjectType" />
<xsl:attribute name="label" select="$ObjectType" />
<xsl:attribute name="minCount">1</xsl:attribute>
<xsl:attribute name="maxCount">1</xsl:attribute>
<xsl:apply-templates select="key('complexType', $ObjectType)" />
</xsl:element>
</xsl:template>
<xsl:template match="*:complexType">
<xsl:value-of select="@name" />
<xsl:apply-templates select="*/*:element" />
</xsl:template>
<xsl:template match="*:element">
<xsl:apply-templates select="key('complexType', @type)" />
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/27856432
复制相似问题