xml - XSLT:将 namespace 设置为第一个属性

标签 xml xslt powershell namespaces transformation

xsl 转换后结果 xml 文件中的命名空间位置有问题。

我的转换样式表看起来像

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output indent="yes" method="xml" />
<xsl:template match="/">

<xsl:element name="SmartDriveUpdates">
  <xsl:attribute name="xsi:noNamespaceSchemaLocation">
    <xsl:text>LightSpeedXMLSchema.xsd</xsl:text>
  </xsl:attribute>
...
</xsl:element>

在输出 xml 文件中,我想获取根节点作为

<SmartDriveUpdates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LightSpeedXMLSchema.xsd">

但是我有

<SmartDriveUpdates xsi:noNamespaceSchemaLocation="LightSpeedXMLSchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

我还尝试将 xsl 样式表中的根节点预编码为

<SmartDriveUpdates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LightSpeedXMLSchema.xsd">
  ...
</SmartDriveUpdates>

但我得到了同样错误的结果。

使用 system.xml.xsl.xslcompiledtransform .NET 类中的 Transform 方法将转换应用于 xml 文件。为此,我使用 PowerShell:

Function Convert-WithXslt($originalXmlFilePath, $xslFilePath, $outputFilePath) {
## Simplistic error handling
$xslFilePath = Resolve-Path $xslFilePath
If( -not (Test-Path $xslFilePath) ) { 
    Throw "Can't find the XSL file" 
} 

$originalXmlFilePath = Resolve-Path $originalXmlFilePath
If( -not (Test-Path $originalXmlFilePath) ) { 
    Throw "Can't find the XML file" 
}

#$outputFilePath = Resolve-Path $outputFilePath
If( -not (Test-Path (Split-Path $originalXmlFilePath)) ) {
    Throw "Can't find the output folder" 
} 

## Get an XSL Transform object (try for the new .Net 3.5 version first)
$EAP = $ErrorActionPreference
$ErrorActionPreference = "SilentlyContinue"

$script:xslt = New-Object system.xml.xsl.xslcompiledtransform
Trap [System.Management.Automation.PSArgumentException] 
{  # no 3.5, use the slower 2.0 one
    $ErrorActionPreference = $EAP
    $script:xslt = New-Object system.xml.xsl.xsltransform
}
$ErrorActionPreference = $EAP

## load xslt file
$xslt.load( $xslFilePath )

## transform 
$xslt.Transform( $originalXmlFilePath, $outputFilePath )
}

有人可以帮我解决这个问题吗?

谢谢

最佳答案

属性和 namespace 声明属性的顺序并不重要,我认为您在使用 XSLT 时无法定义该顺序。为什么顺序对您很重要?

关于xml - XSLT:将 namespace 设置为第一个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5324948/

相关文章:

list - 如何创建输出列表(不是表格)的对象

java - 将 xml 字符串作为文档传递返回 null

html - XSLT 解析存储在属性中的转义 HTML,并将该属性的内容转换为元素的内容

parsing - Powershell中的文本解析: Identify a target line and parse the next X lines to create objects

Windows Server 2012 R2 长文件路径不起作用?

XSLT 根据元素值删除重复节点

python - 没有名为 urllib3 的模块

xml - 使用XmlSlurper/GPath查找不区分大小写的xml元素值?

python - 使用 Python 解析 XML 数据

iphone - Safari 中不支持 XSLTProcessor() 吗?