2013-07-25 3 views
0

У меня есть ниже файл XML созданЗначение необходимо удалить из файла XML с помощью XSLT

<MainTag> 
    <MainSubTag> 
     <Tag1>Value1</Tag1> 
     <Tag2>Value2</Tag2> 
    </MainSubTag> 
</MainTag> 
<MainTag> 
    <MainSubTag> 
     <Tag1>Value1</Tag1> 
     <Tag2>Value2</Tag2> 
    </MainSubTag> 
</MainTag> 

Ожидаемый результат:

<MainTag> 
    <MainSubTag> 
     <Tag2>Value2</Tag2> 
    </MainSubTag> 
    <MainSubTag> 
     <Tag2>Value2</Tag2> 
    </MainSubTag> 
</MainTag> 

Я попытался ниже XSLT

<?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="/MainTag"> 
    <MainTag> 
<xsl:for-each select="MainSubTag"> 
    <MainSubTag> 
    <xsl:apply-templates select="./*[not(name()='Tag1')]"/> 
    </MainSubTag> 
</xsl:for-each> 
    </MainTag> 
</xsl:template> 

<xsl:template match="MainSubTag"> 
    <MainSubTag> 
     <xsl:apply-templates select="./@*[not(name()='Tag1')]"/> 
    </MainSubTag> 
</xsl:template> 
<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

    </xsl:stylesheet> 

Но его не работает. Он бросает ниже ошибки

Fatal: Xalan фатальная ошибка Ожидаемый комментарий или обработка инструкций

ответ

0

Там много источников в этом сайте. Это должно помочь:

remove xml tags with XSLT

+0

< xsl: template match = "Tag1" /> user2619779

+0

Я пробовал выше, но я все еще получаю такие ошибки, как Fatal: фатальная ошибка Xalan. Ожидаемый комментарий или инструкция по обработке – user2619779

+0

Поскольку повторения MainTag и MainsubTag и Tag1 должны быть найденный между каждым циклом, мы должны использовать для каждого? – user2619779

1

Я предполагаю, что, как уже упоминалось, вы должны иметь корневой элемент в вашем входе XML (без которых преобразование потерпит неудачу, как вы нашли).

Учитывая следующий XML:

<RootElement> 
    <MainTag> 
    <MainSubTag> 
     <Tag1>Value1</Tag1> 
     <Tag2>Value2</Tag2> 
    </MainSubTag> 
    </MainTag> 
    <MainTag> 
    <MainSubTag> 
     <Tag1>Value1</Tag1> 
     <Tag2>Value2</Tag2> 
    </MainSubTag> 
    </MainTag> 
</RootElement> 

Это XSL таблицы стилей:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:strip-space elements="*"/> 

    <!-- The identity transform. --> 
    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- Don't show the RootElement in the output. --> 
    <xsl:template match="RootElement"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <!-- When we output the first MainTag element, find all the elements at 
     the same depth as its children (e.g. elements under other MainTag 
     elements) and process with them. --> 
    <xsl:template match="MainTag[position() = 1]"> 
    <xsl:copy> 
     <xsl:apply-templates select="(following-sibling::* | self::*)/*"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- Only output the first MainTag element. --> 
    <xsl:template match="MainTag[position() != 1]"/> 

    <!-- Don't output Tag1 elements. --> 
    <xsl:template match="Tag1"/> 

</xsl:stylesheet> 

Производит нужный XML:

<MainTag> 
    <MainSubTag> 
     <Tag2>Value2</Tag2> 
    </MainSubTag> 
    <MainSubTag> 
     <Tag2>Value2</Tag2> 
    </MainSubTag> 
</MainTag> 
+0

Возможно, вы захотите изменить шаблон-шаблон в шаблоне для 'RootElement' на' '.Вы не хотите применять шаблоны к '@ *', если вы не выводите элемент. Кроме того, 'node()' содержит текст, и это не будет хорошо, так как RootElement является корневым элементом. –

+0

Отмечено и отредактировано. Спасибо. –

+0

+1 Для хорошего ответа. –

Смежные вопросы