|
I'm looking for a way to properly set xsl:match="..." or xsl:value-of select="..."
My files are:
test.xsl
="1.0"="Windows-1250"
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
</head>
<body>
<h2>SingleRow1</h2>
<table border="1px" bgcolor="yellow">
<tr>
<td><xsl:value-of select="DocumentContent/section['SingleRow1']/row/FieldA"/>
</td>
<td><xsl:value-of select="DocumentContent/section['SingleRow1']/row/FieldB"/>
</td>
<td><xsl:value-of select="DocumentContent/section['SingleRow1']/row/FieldC"/>
</td>
<td><xsl:value-of select="DocumentContent/section['SingleRow1']/row/FieldD"/>
</td>
</tr>
</table>
<br/>
<h2>SingleRow2</h2>
<table border="1px" bgcolor="yellow">
<tr>
<td><xsl:value-of select="DocumentContent/section['SingleRow2']/row/FieldA"/>
</td>
<td><xsl:value-of select="DocumentContent/section['SingleRow2']/row/FieldB"/>
</td>
<td><xsl:value-of select="DocumentContent/section['SingleRow2']/row/FieldC"/>
</td>
<td><xsl:value-of select="DocumentContent/section['SingleRow2']/row/FieldD"/>
</td>
</tr>
</table>
<h2>MulitRow1</h2>
<table border="1px" bgcolor="lightgreen">
<xsl:for-each select="DocumentContent/section['MultiRow1']/row">
<tr>
<td><xsl:value-of select="Field1"/>
</td>
<td><xsl:value-of select="Field2"/>
</td>
<td><xsl:value-of select="Field3"/>
</td>
<td><xsl:value-of select="Field4"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
test.xml
="1.0"="windows-1250"
="text/xsl"="test.xsl"
<DocumentContent>
<section name="SingleRow1">
<row>
<FieldA>A</FieldA>
<FieldB>B</FieldB>
<FieldC>C</FieldC>
<FieldD>D</FieldD>
</row>
</section>
<section name="SingleRow2">
<row>
<FieldA>E</FieldA>
<FieldB>F</FieldB>
<FieldC>G</FieldC>
<FieldD>H</FieldD>
</row>
</section>
<section name="MultiRow1">
<row>
<Field1>1</Field1>
<Field2>2</Field2>
<Field3>3</Field3>
<Field4>4</Field4>
</row>
<row>
<Field1>5</Field1>
<Field2>6</Field2>
<Field3>7</Field3>
<Field4>8</Field4>
</row>
<row>
<Field1>9</Field1>
<Field2>10</Field2>
<Field3>11</Field3>
<Field4>12</Field4>
</row>
</section>
</DocumentContent>
The result should be:
SingleRow1
A B C D
SingleRow1
E F D H
MultiRow2
1 2 3 4
5 6 7 8
9 10 11 12
but instead of this i get twice
SingleRow1
A B C D
SingleRow2
A B C D
MultiRow1
3 times empty row
1 2 3 4
5 6 7 8
9 10 11 12
|
|
|
|
|
You can't just use the value of an attribute on the node as an indexer into the collection of nodes. You need to specify the attribute name and a comparison operator:
DocumentContent/section[<ins>@name = </ins>'SingleRow1']/row/FieldA
Your current XSL is matching every <section> node, which is why you're getting two blank lines at the start of your <xsl:for-each> block - one for "SingleRow1", and one for "SingleRow2".
="1.0"="Windows-1250"
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
</head>
<body>
<h2>SingleRow1</h2>
<table border="1px" bgcolor="yellow">
<tr>
<td><xsl:value-of select="DocumentContent/section[@name = 'SingleRow1']/row/FieldA"/>
</td>
<td><xsl:value-of select="DocumentContent/section[@name = 'SingleRow1']/row/FieldB"/>
</td>
<td><xsl:value-of select="DocumentContent/section[@name = 'SingleRow1']/row/FieldC"/>
</td>
<td><xsl:value-of select="DocumentContent/section[@name = 'SingleRow1']/row/FieldD"/>
</td>
</tr>
</table>
<br/>
<h2>SingleRow2</h2>
<table border="1px" bgcolor="yellow">
<tr>
<td><xsl:value-of select="DocumentContent/section[@name = 'SingleRow2']/row/FieldA"/>
</td>
<td><xsl:value-of select="DocumentContent/section[@name = 'SingleRow2']/row/FieldB"/>
</td>
<td><xsl:value-of select="DocumentContent/section[@name = 'SingleRow2']/row/FieldC"/>
</td>
<td><xsl:value-of select="DocumentContent/section[@name = 'SingleRow2']/row/FieldD"/>
</td>
</tr>
</table>
<h2>MulitRow1</h2>
<table border="1px" bgcolor="lightgreen">
<xsl:for-each select="DocumentContent/section[@name = 'MultiRow1']/row">
<tr>
<td><xsl:value-of select="Field1"/>
</td>
<td><xsl:value-of select="Field2"/>
</td>
<td><xsl:value-of select="Field3"/>
</td>
<td><xsl:value-of select="Field4"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you, Richard so much
Last few days i was struggle with it
section[@thankyou='thank you']
|
|
|
|
|
Finally tired of the "System.Xml.XmlValidatingReader is obsolete" message, I undertook to replace it with the recommended XmlReader. The obsolescence documentation from MS wasn't much help, but I did manage to create code that compiled, as follows:
string nameSpace = GetSchemaNamespace(_XmlDocumentStream);
XmlTextReader schemaReader = new XmlTextReader(_SchemaStream);
XmlSchemaSet xsc = new XmlSchemaSet();
xsc.Add(nameSpace, schemaReader);
XmlNameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
XmlParserContext parserContext = new XmlParserContext(nt, nsmgr, null, XmlSpace.None);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.Schemas.Add(xsc);
settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
XmlReader reader = XmlReader.Create(_XmlDocumentStream, settings, parserContext);
And then I found these two things I don't understand:
1. First, the Create failed because this attribute in the xsd wasn't defined.
<xs:attribute ref="xml:space"/>
There was no complaint over this from the obsolete class. From the error message, I assume that this line previously covered that definition:
<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd"/>
What changed such that this is now a fatal error?
2. I removed the references to the above attribute so I could get a reader created and ran our set of unit tests for this process. The next surprise was that the reader doesn't handle validation errors the same as before. (a) Undefined attributes used to be Warnings. Now they are Errors. So what is now considered a warning? (b) It used to continue on from errors, allowing us to compile a list of errors and warnings for the XML being validated. Now, it bails out after the first error and I never get more than one invocation of the ValidationEventHandler. The test case that used to yield 43 errors and 21 warnings now produces a single error (and the test therefore fails). Is there a way to get the former behavior back?
modified 6-Nov-14 13:12pm.
|
|
|
|
|
2) Try adding handlers directly to the reader and see if you get any further; e.g.
XmlSerializer serializer = new XmlSerializer( typeof( ... ) );
serializer.UnknownNode += new XmlNodeEventHandler( serializer_UnknownNode );
serializer.UnknownAttribute += new XmlAttributeEventHandler( serializer_UnknownAttribute );
|
|
|
|
|
I don't see a way to use that suggestion with the parts that I have. The errors of interest are not going to be found by the default serializer; they are schema conformance errors, relating to a specific xsd, and not plain XML errors.
modified 3-Nov-14 14:36pm.
|
|
|
|
|
Hey, Moderator, I now think this would be more appropriate for the XML forum. Do you agree?
|
|
|
|
|
I used Web Browser(html) the "string" to creat XML?
-- modified 12-Oct-14 23:49pm.
|
|
|
|
|
what is your exact question?
modified 20-Sep-20 21:01pm.
|
|
|
|
|
I have some data in Excel which represents structured lists to be imported into a SQL database in an XML format. I am not so familiar with the nuances of XML but I am pretty good with VBA. I need to generate an XML file that looks like this.
="1.0"
<!DOCTYPE xml>
-<xml>
-<transactions>
-<transaction vaultname="Type in the name of the vault here" date="123459" type="import_lists">
-<list name="Materials">
<item item_name="Ceramic"/>
<item item_name="Ferrous Metal"/>
<item item_name="Nonferrous Metal"/>
<item item_name="Superalloy"/>
</list>
-<list name="Ceramics">
<item item_name="Boride"/>
<item item_name="Carbide"/>
<item item_name="Nitride"/>
<link link_value="Ceramic" list_variable="Material Type" source_variable="My Material"/>
</list>
-<list name="Ferrous Metals">
<item item_name="Stainless Steel"/>
<item item_name="Carbon Steel"/>
<item item_name="Tool Steel"/>
<link link_value="Ferrous Metal" list_variable="Material Type" source_variable="My Material"/>
</list>
-<list name="Nonferrous Metals">
<item item_name="Aluminium Alloy"/>
<item item_name="Copper Alloy"/>
<item item_name="Tin Alloy"/>
<link link_value="Nonferrous Metal" list_variable="Material Type" source_variable="My Material"/>
</list>
-<list name="Superalloys">
<item item_name="Cobalt Base"/>
<item item_name="Iron Base"/>
<item item_name="Nickel base"/>
<link link_value="Superalloy" list_variable="Material Type" source_variable="My Material"/>
</list>
</transaction>
</transactions>
</xml>
I have the data in Excel and I would like to use VBA code similar to the below, but I am having a devil of a time getting it to output as shown above. When I change the tags to what I need in the sample output the result is just a mess. I am sure its my limited exposure to XML that has me stumbling. If someone could show me a sample set of the VBA that would output some of the above it would get me rolling again.
Dim FSO As Object
Dim NewFile As Object
Dim FullPath As String
Dim XMLFileText As String
FullPath = "C:\Users\Administrator\Desktop\Guy2.xml"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set NewFile = FSO.CreateTextFile(FullPath, 1, 1)
XMLFileText = ""
XMLFileText = XMLFileText & "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" & vbNewLine
XMLFileText = XMLFileText & "<DATAROOT>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "<DATA>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "<c10>" & Range("C10") & "</c10>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "<D10>" & Range("D10") & "</D10>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "<E10>" & Range("E10") & "</E10>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "</DATA>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "<DATA>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "<C11>" & Range("C11") & "</C11>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "<D11>" & Range("D11") & "</D11>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "<E11>" & Range("E11") & "</E11>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "</DATA>" & vbNewLine
NewFile.Write (XMLFileText)
XMLFileText = "</DATAROOT>" & vbNewLine
NewFile.Write (XMLFileText)
NewFile.Close
modified 30-Sep-14 22:30pm.
|
|
|
|
|
You'll probably get better results if you use the MSXML components[^] to generate your XML document. Apart from anything else, it will take care of properly encoding your string values.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
XML
="1.0"="UTF-8"
<catalog>
<example>
:20:FT13261793408907
N23B:CRED
SA32A:130918USD111670,00
</example>
</catalog>
XSLT
="1.0"="UTF-8"
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="catalog">
<tr>
<td><xsl:value-of select="example"> </td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Current OUTPUT
:20:FT13261793408907 N23B:CRED SA32A:130918USD111670,00
Desired OUTPUT
:20:FT13261793408907
N23B:CRED
SA32A:130918USD111670,00
output must not be in a same line its must be as shown in the desired o/p
|
|
|
|
|
You don't mention if there can be one or more example nodes within each catalog node.
This code works if there are many catalog's with one example node.
="1.0"="utf-8"
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="text" indent="no"/>
<xsl:template match="catalog">
<xsl:value-of select="example"/>
</xsl:template>
</xsl:stylesheet>
And this code works if there are many catalog nodes and many example nodes.
="1.0"="utf-8"
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="text" indent="no"/>
<xsl:template match="catalog">
<xsl:apply-templates select="example" />
</xsl:template>
<xsl:template match="example">
<xsl:value-of select="node()"/>
</xsl:template>
</xsl:stylesheet>
XSLT is recursive by nature, so no need for the loop.
If you want text output, I don't understand the HTML tags in your example code.
|
|
|
|
|
Hi all, I have already raised my question in different area of this forum. Please have a look at my question from the below link for more information. Could you please help me to generate xml structure with mutiple namespces. TIA.
Multiple namespace in xslt[^]
|
|
|
|
|
Please do not do this; post your question in one place only.
|
|
|
|
|
So, should I post it here instead of quick answer section?
|
|
|
|
|
|
Hello,
I'd like to serialize an Objekt with XmlSerializer. The Objekt is a kind of treeview with lots of public properties like this:
public object Instance;
Currently I have different kind of classes stored in the Instance objects, all inheriting from the same base class. If I want to serialize these, I need to tell the XmlSerializer in an XmlAttributeOverrides object about the different Instance object classes. I collect this information dynamically before starting the XmlSerializer.
Setting a breakpoint just before that I can check the Overrides object which looks like this:
- Overrides {System.Xml.Serialization.XmlAttributeOverrides} System.Xml.Serialization.XmlAttributeOverrides
- Non-Public members
- types Count = 5 System.Collections.Hashtable
- [{CTrModules.MDataSaveXML}] {System.Collections.Hashtable}
+ Key {Name = "MDataSaveXML" FullName = "CTrModules.MDataSaveXML"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
+ Raw View
- [{CTrModules.QSM_Var_Dict}] {System.Collections.Hashtable}
+ Key {Name = "QSM_Var_Dict" FullName = "CTrModules.QSM_Var_Dict"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
+ Raw View
- [{System.String}] {System.Collections.Hashtable}
+ Key {Name = "String" FullName = "System.String"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
+ Raw View
- [{CTrModules.MHandleException}] {System.Collections.Hashtable}
+ Key {Name = "MHandleException" FullName = "CTrModules.MHandleException"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
+ Raw View
- [{FFV_Cockpit.InternalExcHandler}] {System.Collections.Hashtable}
+ Key {Name = "InternalExcHandler" FullName = "FFV_Cockpit.InternalExcHandler"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
Now here's my problem: I get a InvalidOperationException: "The type CTrModules.MDataSaveXML was not expected."
Looking through the Overrides above, CTrModules.MDataSaveXML was handed over to the serializer along with all other classes. All other classes can be serialized, but not this one. All classes have the Serializable attribute and are inheriting the same base class.
I'm at a loss here. Why is CTrModules.MDataSaveXML not expected?
I'm using C# in Visual Studio Espress 2013 for Windows Desktop, the target framework is .NET Framework 4.
TIA,
Christian
|
|
|
|
|
|
Hi Bernhard,
Bernhard Hiller wrote: Did you add the [XmlInclude(typeof(CTrModules.MDataSaveXML))]
attribute to the base class
I don't really want to go with that option, because then I have to remember to add such an include for any class I derive from the base class.
But I tried it just for test purposes: I get the same problem.
The option I chose to use is this one:
Bernhard Hiller wrote: or pass an array of types to the constructor of the serializer?
I wrote a method to gather all types in an array and I pass this array to the serializer.
I set a breakpoint at the call of the serializer and inspected this array:
- Overrides {System.Xml.Serialization.XmlAttributeOverrides} System.Xml.Serialization.XmlAttributeOverrides
- Non-Public members
- types Count = 5 System.Collections.Hashtable
- [{CTrModules.MDataSaveXML}] {System.Collections.Hashtable}
+ Key {Name = "MDataSaveXML" FullName = "CTrModules.MDataSaveXML"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
+ Raw View
- [{CTrModules.QSM_Var_Dict}] {System.Collections.Hashtable}
+ Key {Name = "QSM_Var_Dict" FullName = "CTrModules.QSM_Var_Dict"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
+ Raw View
- [{System.String}] {System.Collections.Hashtable}
+ Key {Name = "String" FullName = "System.String"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
+ Raw View
- [{CTrModules.MHandleException}] {System.Collections.Hashtable}
+ Key {Name = "MHandleException" FullName = "CTrModules.MHandleException"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
+ Raw View
- [{FFV_Cockpit.InternalExcHandler}] {System.Collections.Hashtable}
+ Key {Name = "InternalExcHandler" FullName = "FFV_Cockpit.InternalExcHandler"} object {System.RuntimeType}
- Value Count = 1 object {System.Collections.Hashtable}
+ ["Instance"] {System.Xml.Serialization.XmlAttributes}
It contains all classes, including CTrModules.MDataSaveXML. All other classes can be serialized, but not this one.
CU,
Christian
|
|
|
|
|
Hello, I have a xml file and try to select nodes with a xpath query based on multiple attribute values, but can't get it to work. please tell me the correct xpath.
here's part of the xml:
<AppInt ID="91" Type="Get_Rcp_Rel_Content">
<Tags>
<tag ID="300" ROLE="REQTRG" PLCID="PLC008" DB="113" ADDRESS="36" LENGTH="1" DATATYPE="INT" />
<tag ID="301" ROLE="RSPTRG" PLCID="PLC008" DB="113" ADDRESS="38" LENGTH="1" DATATYPE="INT" />
<tag ID="302" ROLE="modeID" PLCID="PLC008" DB="113" ADDRESS="40" LENGTH="1" DATATYPE="INT" />
<tag ID="303" ROLE="jobID" PLCID="PLC008" DB="113" ADDRESS="42" LENGTH="1" DATATYPE="INT" />
<tag ID="304" ROLE="RcpRelID" PLCID="PLC008" DB="113" ADDRESS="44" LENGTH="1" DATATYPE="INT" />
<tag ID="305" ROLE="Enable" PLCID="PLC008" DB="113" ADDRESS="46" LENGTH="1" BIT="0" DATATYPE="BOOL" />
<tag ID="306" ROLE="FillingTime" PLCID="PLC008" DB="113" ADDRESS="48" LENGTH="1" DATATYPE="LONG" />
<tag ID="307" ROLE="FillingRPM" PLCID="PLC008" DB="113" ADDRESS="52" LENGTH="1" DATATYPE="REAL" />
</Tags>
<Setpoints>
<Setpoint JobID="1002" ModeID ="1" Name="Enable" Value="True"/>
<Setpoint JobID="1002" ModeID ="1" Name="FillingTime" Value="5500"/>
<Setpoint JobID="1002" ModeID ="1" Name="FillingRPM" Value="53.2"/>
</Setpoints>
</AppInt>
and here's my xpath query:
//AppInt [@ID="91"]/Setpoints [Setpoint/@JobID="1002" and Setpoint/@ModeID="1"]/@Name
|
|
|
|
|
Which nodes are you trying to select?
|
|
|
|
|
You asked the right question: I was trying to select the setpoint nodes, so i must remove the @Name.
|
|
|
|
|
Try this:
//AppInt[@ID="91"]/Setpoints/Setpoint[@JobID="1002" and @ModeID="1"]/@Name
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello!
I have no possibility to use following-sibling axis.
How can I get the same functionality in XPath without using axes? Is it real?
Thanks in advance.
|
|
|
|