/ / Chcę zastąpić nazwę węzła danego węzła, z wyjątkiem najbardziej wewnętrznych węzłów potomnych? - xml, xslt, xpath

chcę zastąpić nazwę węzła danego węzła z wyjątkiem najbardziej wewnętrznych węzłów podrzędnych? - xml, xslt, xpath

Wprowadź XML

<topicref outputclass="Dx:Appendix" href="appendix_topic_a.dita">
<topicref outputclass="Dx:Appendix" href="appendix_topic_b.dita">
<topicref outputclass="Dx:Appendix" href="appendix_topic_c.dita"/>
</topicref>
<topicref outputclass="Dx:Appendix" href="appendix_topic_d.dita">
<topicref outputclass="Dx:Appendix" href="appendix_topic_e.dita"/>
</topicref>
</topicref>
<topicref outputclass="Dx:Appendix" href="appendix_topic_f.dita"/>

Wydajność

<appendix class="Dx:Appendix" href="appendix_topic_a.dita">
<appendix class="Dx:Appendix" href="appendix_topic_b.dita">
<topicref class="Dx:Appendix" href="appendix_topic_c.dita"/>
</appendix>
<appendix class="Dx:Appendix" href="appendix_topic_d.dita">
<topicref class="Dx:Appendix" href="appendix_topic_e.dita"/>
</appendix>
</appendix>
<appendix class="Dx:Appendix" href="appendix_topic_f.dita"/>

Zasadniczo muszę zmienić nazwę węzła <topicref> do <appendix> jeśli to nie jest najgłębsze <topicref>. A jeśli jest to samodzielny węzeł, jak w przypadku <topicref> z href="appendix_topic_f.dita", Muszę to zmienić na <appendix> który jest tagiem samozamykającym. Ponadto nazwa atrybutu outputclass musi zostać zmieniony na class utrzymując tę ​​samą wartość.

Odpowiedzi:

0 dla odpowiedzi № 1

Jeśli pozwolę sobie na przekształcenie twoich wymagań jako: zmień nazwę topicref gdyby:

  • ma inny topicref jako dziecko; lub
  • jego rodzic nie jest inny topicref

następnie docieram do:

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="topicref[topicref or not(parent::topicref)]">
<appendix>
<xsl:apply-templates select="@*|node()"/>
</appendix>
</xsl:template>

</xsl:stylesheet>

Zastosowano do następujących dobrze uformowany wejście testowe:

XML

<root>
<topicref outputclass="Dx:Appendix" href="appendix_topic_a.dita">
<topicref outputclass="Dx:Appendix" href="appendix_topic_b.dita">
<topicref outputclass="Dx:Appendix" href="appendix_topic_c.dita"/>
</topicref>
<topicref outputclass="Dx:Appendix" href="appendix_topic_d.dita">
<topicref outputclass="Dx:Appendix" href="appendix_topic_e.dita"/>
</topicref>
</topicref>
<topicref outputclass="Dx:Appendix" href="appendix_topic_f.dita"/>
</root>

temu wynik będzie:

<?xml version="1.0" encoding="utf-8"?>
<root>
<appendix outputclass="Dx:Appendix" href="appendix_topic_a.dita">
<appendix outputclass="Dx:Appendix" href="appendix_topic_b.dita">
<topicref outputclass="Dx:Appendix" href="appendix_topic_c.dita"/>
</appendix>
<appendix outputclass="Dx:Appendix" href="appendix_topic_d.dita">
<topicref outputclass="Dx:Appendix" href="appendix_topic_e.dita"/>
</appendix>
</appendix>
<appendix outputclass="Dx:Appendix" href="appendix_topic_f.dita"/>
</root>