[英]Sort a specific XML File with XSLT by multiple attributes
Here is the XML structure. 这是XML结构。 I have to sort/order by category name first, then by preference name and then sort the value list itself. 我必须先按类别名称排序/排序,然后再按首选项名称排序,然后再对值列表本身进行排序。 I am new to xslt. 我是xslt的新手。 How to achieve multiple ordering? 如何实现多重订购?
<?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>
How can I apply a xslt via Browser. 如何通过浏览器应用xslt。 How to reference the xslt in the XML? 如何在XML中引用xslt?
Edit: I now tried something like the following. 编辑:我现在尝试类似以下内容。 First Problem there are missing some attributes in the Output like the descriptions. 第一个问题是输出中缺少一些描述等属性。 I think I don't understand the apply-template right. 我认为我不了解申请模板的权利。 Second Problem is that the values are not sorted. 第二个问题是值没有排序。 The category name and the Preference name is sorted 类别名称和首选项名称已排序
<?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>
You haven't explained your requirements very clearly: for example there's no hint what output you want to produce (is it HTML?). 您没有非常清楚地说明您的要求:例如,没有任何提示您想要产生什么输出(是HTML吗?)。 If you want to sort the preferences within a category, you can do 如果要对类别中的首选项进行排序,可以执行
<xsl:template match="category">
<xsl:apply-templates select="preference">
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:template>
and similarly for the other things you want to sort. 同样,对于您要排序的其他内容也是如此。
Try it this way: 尝试这种方式:
<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.