无法使用<code>

时间:2018-02-15 12:29:39

标签: xml xslt

I know that this question might be a very basic and common one (some of you say that it is questioned everyday). But I am very new to XSLT, and I really really need your help :(

I need to transform an XML to XML using XSLT

The input XML :

<?xml version="1.0" encoding="UTF-8"?>
<SyncItemMaster xmlns="XMLNS-A" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xsi:schemaLocation="XMLNS-A http://schema.com/2.13.0/SyncItemMaster.xsd" releaseID="9.2" versionID="2.13.0">
<ItemMaster>
        <ItemMasterHeader>
            <Classification>
                <Codes>
                    <Code listID="Item Types" sequence="1">Cost</Code>
                    <Code listID="Item Groups" sequence="2">4ET</Code>
                    <Code listID="MRO Classes" sequence="3">*</Code>
                </Codes>
            </Classification>
        </ItemMasterHeader>
    </ItemMaster>
</DataArea>

I tried the XSLT as follow :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:my="XMLNS-A"
xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
    indent="yes" />
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>
<xsl:template match="//my:SyncItemMaster/my:DataArea/my:ItemMaster/my:ItemMasterHeader/my:Classification/my:Codes">
    <xsl:variable name="service2"select="//my:SyncItemMaster/my:DataArea/my:ItemMaster/my:ItemMasterHeader/my:ServiceIndicator"/>
    <xsl:variable name="type2"          select="//my:SyncItemMaster/my:DataArea/my:ItemMaster/my:ItemMasterHeader/my:Classification/my:Codes/my:Code"/>
    <Code listID="Item Types" sequence="1">
        <xsl:choose>
            <xsl:when test="($service2='true' and $type2='Cost')">
                <xsl:text>Purchased</xsl:text>              
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="ServiceIndicator">
                    <xsl:value-of select="$service2" />
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </Code>
</xsl:template>

But now, I have this result :

<?xml version="1.0" encoding="UTF-8"?>
<SyncItemMaster xmlns="XMLNS-A" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xsi:schemaLocation="XMLNS-A http://schema.com/2.13.0/SyncItemMaster.xsd" releaseID="9.2" versionID="2.13.0">
<ItemMaster>
        <ItemMasterHeader>
            <Classification>
                <Codes>
                    <Code listID="Item Types" sequence="1" xmlns:my="XMLNS-A">Purchased</Code>
                    <Code listID="Item Groups" sequence="2">4ET</Code>
                    <Code listID="MRO Classes" sequence="3">*</Code>
                </Codes>
            </Classification>
        </ItemMasterHeader>
    </ItemMaster>
</DataArea>

Note : please ignore the variables, because I already cropped the XML for simplicity.

I am trying to get rid of the namespace on the "Item Types" above. I have look around for clues in Stackoverflow and tried it, but nothing works.

Please help me..

Thank you

2 个答案:

答案 0 :(得分:0)

好吧我自己修复了你的代码我不确定如果你有相同的结构。但我尝试了解决方案,我发现没有这样的错误。

我用过的XML:

<?xml version="1.0" encoding="UTF-8"?>
<my:SyncItemMaster xmlns:my="XMLNS-A" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<my:DataArea>
    <my:ItemMaster>
        <my:ItemMasterHeader>
            <my:ServiceIndicator>true</my:ServiceIndicator>
            <my:Classification>
                <Code listID="Item Types" sequence="1">Purchased</Code>
            </my:Classification>
        </my:ItemMasterHeader>
    </my:ItemMaster>
</my:DataArea>
</my:SyncItemMaster>

XSLT文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="XMLNS-A" 
xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="@* | node()">
<xsl:copy>
    <xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="//my:SyncItemMaster/my:DataArea/my:ItemMaster/my:ItemMasterHeader/my:Classification/my:Codes">
<xsl:variable name="service2" select="//my:SyncItemMaster/my:DataArea/my:ItemMaster/my:ItemMasterHeader/my:ServiceIndicator"/>
<xsl:variable name="type2"          select="my:Code"/>
<Code listID="Item Types" sequence="1">
    <xsl:choose>
        <xsl:when test="($service2='true' and $type2='Cost')">
            <xsl:text>Purchased</xsl:text>              
        </xsl:when>
        <xsl:otherwise>
            <xsl:element name="ServiceIndicator">
                <xsl:value-of select="$service2" />
            </xsl:element>
        </xsl:otherwise>
    </xsl:choose>
</Code>
</xsl:template>
</xsl:stylesheet>

,输出为:

<?xml version="1.0" encoding="UTF-8"?>
<my:SyncItemMaster xmlns:my="XMLNS-A" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<my:DataArea>
    <my:ItemMaster>
        <my:ItemMasterHeader>
            <my:ServiceIndicator>true</my:ServiceIndicator>
            <my:Classification>
                <Code listID="Item Types" sequence="1">Purchased</Code>
            </my:Classification>
        </my:ItemMasterHeader>
    </my:ItemMaster>
</my:DataArea>
</my:SyncItemMaster>

注意:此外,您不必一遍又一遍地使用所有XPath,因为您已经将模板与您已经在该节点中的my:Codes匹配,并且您可以只在内部指定子节点那个模板。

答案 1 :(得分:0)

你已经有了

data

将其扩展到

exclude-result-prefixes="java"

exclude-result-prefixes="java my"

(值#all需要XSLT 2.0 - 你还没有说你正在使用哪个版本。)