简体   繁体   中英

xslt is not transforming generated xml

I am completely new to xslt so please bear with me on this question.

I have an XML file I am generating from sqlpackage.exe in a PowerShell script. This is a cut down version of the output (there are a number nodes but I have only included one here)

 <?xml version="1.0" encoding="utf-8"?> <DeploymentReport xmlns="http://schemas.microsoft.com/sqlserver/dac/DeployReport/2012/02"> <Operations> <Operation Name="Drop"> <Item Value="[dbo].[WORKORDERSchema]" Type="SqlXmlSchemaCollection" /> <Item Value="[///SourceDB/TargetService]" Type="SqlService" /> <Item Value="[Foo].[DF_HistoryRawMatchedPPR_ActionDate]" Type="SqlDefaultConstraint" /> <Item Value="[dbo].[CMLEDGAPPLY_CreateUpdate_CMLEDGAPPLY]" Type="SqlProcedure" /> <Item Value="[///SourceDB/InitiatorService]" Type="SqlService" /> <Item Value="[///General/20141027]" Type="SqlContract" /> <Item Value="[///RequestMessage]" Type="SqlMessageType" /> </Operation> </Operations> </DeploymentReport> 

 I have created an xslt transform for this to generate the output in a table in html - again cut a down version <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html lang="en"> <head> <title>Environment Refresh</title> </head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="DeploymentReport/Operations/Operation[@Name='Drop']"> <p> <table border="1"> <tr> <td colspan="2">Drop</td> </tr> <xsl:for-each select="Item"> <xsl:sort select="@Type" /> <tr> <td><xsl:value-of select="@Type" /></td> <td><xsl:value-of select="@Value" /></td> </tr> </xsl:for-each> </table> </p> </xsl:template> </xsl:stylesheet> 

when I run this I get the following 'empty' html output and I do not know why

 <html lang="en"> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Environment Refresh</title> </head> <body> </body> </html> 

I have tried various options based on my looking around this site but the only way I can get the xslt to work is if I remove xmlns="http://schemas.microsoft.com/sqlserver/dac/DeployReport/2012/02" from the DeploymentReport node on line 2 of the xml.

I have tried putting xmlns="http://schemas.microsoft.com/sqlserver/dac/DeployReport/2012/02" into the xslt as well and also tried using xmlns:t="http://schemas.microsoft.com/sqlserver/dac/DeployReport/2012/02" format as well but with no luck.

Is anyone able to assist me here please? As I said I am completely new to this and this is my first attempt at xslt so any help or suggestions are welcome

The namespace prefix can be declared in the XSLT and in the select the prefix can be used for the different nodes as below

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ms="http://schemas.microsoft.com/sqlserver/dac/DeployReport/2012/02"
    version="1.0">
    ....
    <xsl:template match="ms:DeploymentReport/ms:Operations/ms:Operation[@Name='Drop']">
        ....
        <xsl:for-each select="ms:Item">
            ....
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM