2014-12-03 7 views
0

У меня есть следующий xml и вы хотите создать out put, который содержит только GENRE_1 и GENRE_3 и любые другие идентификаторы книг. Это означает, что элементы GENRE_4, 5 и 6 будут удалены. Я попытался использовать образец xslt, но не понял его. Поблагодарите любую помощь.Как удалить конкретный узел с помощью XSLT

<bookstore xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Payload xmlns:ns0="http://www.themindelectric.com/"> 
    <books xmlns:ns0="http://www.themindelectric.com/collections/"> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">Everyday Italian</title> 
      <author>Giada De Laurentiis</author> 
      <year>2001</year> 
      <price>30.00</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_3</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>TEST_3</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>ANOTHER_1</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_5</id> 
      <title lang="en">XQuery Kick Start</title> 
      <author>James McGovern</author> 
      <author>Per Bothner</author> 
      <author>Kurt Cagle</author> 
      <author>James Linn</author> 
      <author>Vaidyanathan Nagarajan</author> 
      <year>2003</year> 
      <price>49.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">Learning XML</title> 
      <author>Erik T. Ray</author> 
      <year>2005</year> 
      <price>39.95</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">XQuery Kick Start</title> 
      <author>James McGovern</author> 
      <author>Per Bothner</author> 
      <author>Kurt Cagle</author> 
      <author>James Linn</author> 
      <author>Vaidyanathan Nagarajan</author> 
      <year>2007</year> 
      <price>49.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_6</id> 
      <title lang="en">Learning Java</title> 
      <author>Testing</author> 
      <year>2005</year> 
      <price>39.95</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_4</id> 
      <title lang="en">XQuery Kick Start</title> 
      <author>James McGovern</author> 
      <author>Per Bothner</author> 
      <author>Kurt Cagle</author> 
      <author>James Linn</author> 
      <author>Vaidyanathan Nagarajan</author> 
      <year>2007</year> 
      <price>49.99</price> 
     </book> 
    </books> 
</Payload> 

ЗАПЛАНИРОВАННЫХ

<bookstore xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Payload xmlns:ns0="http://www.themindelectric.com/"> 
    <books xmlns:ns0="http://www.themindelectric.com/collections/"> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">Everyday Italian</title> 
      <author>Giada De Laurentiis</author> 
      <year>2001</year> 
      <price>30.00</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_3</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>TEST_3</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>ANOTHER_1</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">Learning XML</title> 
      <author>Erik T. Ray</author> 
      <year>2005</year> 
      <price>39.95</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">XQuery Kick Start</title> 
      <author>James McGovern</author> 
      <author>Per Bothner</author> 
      <author>Kurt Cagle</author> 
      <author>James Linn</author> 
      <author>Vaidyanathan Nagarajan</author> 
      <year>2007</year> 
      <price>49.99</price> 
     </book> 

    </books> 
</Payload> 

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns0="http://www.themindelectric.com"> 

<xsl:template match="node() | @*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="/bookstore/Payload/books/book[starts-with(id,'GENRE')]"> 
<xsl:call-template name="genre"/> 
</xsl:template> 
<xsl:template name="genre"> 
<xsl:choose> 
    <xsl:when 
     test="count(/bookstore/Payload/books/book[id='GENRE_1']) != 0 or 
count(/bookstore/Payload/books/book[id='GENRE_3']) != 0"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*" /> 
     </xsl:copy> 
    </xsl:when> 
    <xsl:otherwise /> 
</xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

ответ

0

Если я правильно понимаю ваше новое требование, вы хотите, чтобы исключить любую книгу, которая имеет идентификатор, который начинается с GENRE, за исключением GENRE_1 и GENRE_3:

XSLT 1,0

<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:strip-space elements="*"/> 

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

<xsl:template match="book[starts-with(id, 'GENRE') and not(id='GENRE_1' or id='GENRE_3')]"/> 

</xsl:stylesheet> 
+0

Миллион Спасибо. Это решает мои требования – reuel

0

Простое решение просто иметь пустой шаблон соответствия всех книг не имеющие жанр идентификатор, который вы хотите сохранить :

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:ns0="http://www.themindelectric.com"> 
    <xsl:template match="node() | @*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
    </xsl:template> 
    <xsl:template match="book[not(id='GENRE_1' or id='GENRE_3' 
         or id='TEST_3' or id='ANOTHER_1')]"/> 
</xsl:stylesheet> 

Это копирует все, кроме книг, которые не имеют идентификаторов, которые вы хотите сохранить.

Update: Для осветленного требования в обновленном вопросе: сохранить все книги, которые либо имеют GENRE_1 идентификатор или GENRE_3 или иметь идентификатор не соответствует ЖАНРУ:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:ns0="http://www.themindelectric.com"> 
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node() | @*" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="book[starts-with(id, 'GENRE_') and 
         not(contains(id,1) or contains(id,3))]"/> 
</xsl:stylesheet> 

Как уже отмечался в комментариях по michael.hor257k, используя в шаблоне соответствия, не будет работать, если вам придется обрабатывать более 9 жанров. В этом случае правильный совпадение идентификаторов, которые вы хотите сохранить в not() - not(id='GENRE_1' or id='GENRE_3') - как в правильном ответе, заданном michael.hor257k, является правильным подходом. (!)

+0

Спасибо за ваш ответ. Ваше решение будет работать отлично, если я знаю конкретные идентификаторы в xml. Простите мой образец xml. На самом деле, я не буду знать идентификаторы книги, поэтому она не будет работать, если xml содержит TEST_4 или ANOTHER_X. Мое выборочное решение имеет шаблон для всех идентификаторов, начинающихся с GENRE, из всех этих я заинтересован только в GENRE_1 и 3 и любых других идентификаторах без GENRE Book. Cheers – reuel

+0

@reuel Рад, если я был в состоянии помочь вам. Я только что обновил свой ответ для уточненного требования. –

+1

Ваше состояние проходит GENRE_10. –

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