2016-11-03 152 views
0

命名空间,我有以下XML转换XML使用XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<typeNames xmlns="http://www.dsttechnologies.com/awd/rest/v1" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <typeName recordType="case" href="awdServer/awd/services/v1/businessareas/SAMPLEBA/types/SAMPLECASE">SAMPLECASE</typeName> 
    <typeName recordType="folder" href="awdServer/awd/services/v1/businessareas/SAMPLEBA/types/SAMPLEFLD">SAMPLEFLD</typeName> 
    <typeName recordType="source" href="awdServer/awd/services/v1/businessareas/SAMPLEBA/types/SAMPLEST">SAMPLEST</typeName> 
    <typeName recordType="transaction" href="awdServer/awd/services/v1/businessareas/SAMPLEBA/types/SAMPLEWT">SAMPLEWT</typeName> 
</typeNames> 

我想通过使用XSLT如下转换上面的XML:

<response> 
     <results> 

       <source> 
        SAMPLEST 
       </source> 

     </results> 
     </response> 
    </xsl:template> 

我只是想从输入源xml到输出xml。

我想用下面的XML,但无法获得所需的输出XML:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v="http://www.dsttechnologies.com/awd/rest/v1" version="2.0" exclude-result-prefixes="v"> 
    <xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 
    <xsl:strip-space elements="*" /> 
    <!-- identity transform --> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="typeNames"> 
     <response> 
     <results> 

       <source> 
        <xsl:value-of select="source" /> 
       </source> 

     </results> 
     </response> 
    </xsl:template> 
</xsl:stylesheet> 
+0

你已经声明了一个前缀,但你没有使用它。并且在输入XML –

回答

2

一命名空间中输入XML

<typeNames xmlns="http://www.dsttechnologies.com/awd/rest/v1"... 

xmlns把自我+所有子女节点到名称空间中。这个命名空间不需要任何前缀。

二,命名空间中的XSLT

... xmlns:v="http://www.dsttechnologies.com/awd/rest/v1"... 

您与v前缀命名空间(相同的URI源),所以你必须写这个前缀在你的XPath为好。

<xsl:template match="v:typeNames"> 

[XSLT 2.0:您还可以在样式表部分添加xpath-default-namespace="uri",定义所有的XPath表达式默认命名空间。因此,您不必在名称空间前缀。]

三。猜对给定的输入XML

<xsl:value-of select="source" /> -> <typeName recordType="source"..>SAMPLEST</typeName> 

如果您想选择的显示XML节点,你必须写下列之一:

absolute, without any context node: 
/v:typeNames/v:typeName[@recordType = 'source'] 

on context-node typeNames: 
v:typeName[@recordType = 'source'] 

[<xsl:value-of select="..."/>将返回文本节点(S ),例如“SAMPLEST”]


编辑:

如果有两个标签。

首先要做的事情:XSLT 1中的<xsl:value-of只能使用1个节点!如果xpath表达式匹配多个节点,它将只处理第一个节点!

解决它像这样:

... 
<results> 
    <xsl:apply-templates select="v:typeName[@recordType = 'source']"/> 
</results> 
... 

<xsl:template match="v:typeName[@recordType = 'source']"> 
    <source> 
     <xsl:value-of select="."/> 
    </source> 
</xsl:template> 

apply-templatesresults搜索所有typeName..source。匹配模板侦听该节点并创建xml <source>...

+1

中没有'source'元素很好解释。谢谢.. !! –

+0

如果有两个''标签和''作为源,我们想要检索所有'recordType =“source”'的每个值,会发生什么?例如,像这样:' SAMPLEST ORIGINAL' –

+1

请参阅编辑我的答案。 – uL1