[英]Sort a specific XML File with XSLT by multiple attributes
這是XML結構。 我必須先按類別名稱排序/排序,然后再按首選項名稱排序,然后再對值列表本身進行排序。 我是xslt的新手。 如何實現多重訂購?
<?xml version="1.0" encoding="ISO-8859-1"?>
<preferences version="10.0">
<category name="Administration.Access Manager">
<category_description></category_description>
<preference name="ADA_admin_notifier_list" type="String" array="true" disabled="false" protectionScope="Site" envEnabled="false">
<preference_description>Text</preference_description>
</preference>
<preference name="ADA_allow_gov_classification_propagation" type="Logical" array="false" disabled="false" protectionScope="Site" envEnabled="false">
<preference_description>Text</preference_description>
<context name="Teamcenter">
<value>b</value>
<value>c</value>
<value>a</value>
</context>
</preference>
<preference name="ADA_saveas_propagated_license_types" type="String" array="true" disabled="false" protectionScope="Site" envEnabled="false">
<preference_description>TEXT</preference_description>
<context name="Teamcenter">
<value>IP_License</value>
<value>ITAR_License</value>
<value>Exclude_License</value>
</context>
</preference>
</category>
</preferences>
如何通過瀏覽器應用xslt。 如何在XML中引用xslt?
編輯:我現在嘗試類似以下內容。 第一個問題是輸出中缺少一些描述等屬性。 我認為我不了解申請模板的權利。 第二個問題是值沒有排序。 類別名稱和首選項名稱已排序
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="preferences">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="category">
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="category">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="preference">
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="preference">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="preference_desciption"/>
<xsl:apply-templates select="context">
<xsl:sort select="value" data-type="text"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
您沒有非常清楚地說明您的要求:例如,沒有任何提示您想要產生什么輸出(是HTML嗎?)。 如果要對類別中的首選項進行排序,可以執行
<xsl:template match="category">
<xsl:apply-templates select="preference">
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:template>
同樣,對於您要排序的其他內容也是如此。
嘗試這種方式:
<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="/preferences">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="category">
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="category">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="preference">
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="context">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="value">
<xsl:sort select="."/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.